Skip to content

Commit

Permalink
fix: validation of nested fields with validateField (#3783)
Browse files Browse the repository at this point in the history
* Fix validation of nested fields with validateField

* chore: add changeset

---------

Co-authored-by: Jan Sedloň <jansedlon@icloud.com>
Co-authored-by: Jared Palmer <jared@jaredpalmer.com>
  • Loading branch information
3 people authored May 26, 2023
1 parent 9c75a9f commit 35fa4cc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/ten-cups-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Fix validation of deep.dot.path field references when using the `validateField` API.
2 changes: 1 addition & 1 deletion packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
.then((error: any) => {
dispatch({
type: 'SET_FIELD_ERROR',
payload: { field: name, value: error[name] },
payload: { field: name, value: getIn(error, name) },
});
dispatch({ type: 'SET_ISVALIDATING', payload: false });
});
Expand Down
18 changes: 18 additions & 0 deletions packages/formik/test/Field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,24 @@ describe('Field / FastField', () => {
);
}
);

cases('constructs error for a nested field when validateField is called', async (renderField) => {
const validationSchema = Yup.object({
user: Yup.object().shape({
name: Yup.string().required('required')
})
});

const { getFormProps, rerender } = renderField({}, { validationSchema });

rerender();

act(() => {
getFormProps().validateField('user.name');
})

await waitFor(() => expect(getFormProps().errors).toEqual({ user: { name: 'required' }}));
})
});

describe('warnings', () => {
Expand Down

1 comment on commit 35fa4cc

@vercel
Copy link

@vercel vercel bot commented on 35fa4cc May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

formik-docs – ./website

formik-docs-jared.vercel.app
formik-docs-git-master-jared.vercel.app
formik.org
formik-docs.vercel.app
www.formik.org

Please sign in to comment.