Skip to content

Commit

Permalink
revert: "feat(validationSchema): support yup transforms in validate &…
Browse files Browse the repository at this point in the history
… submit (#3796)" (#3815)

Fixes #3809

Will consider this for a future major release.

This reverts commit 2f53b70.
  • Loading branch information
quantizor committed May 31, 2023
1 parent 708bcb2 commit 187e47d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 88 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-radios-clap.md
@@ -0,0 +1,5 @@
---
'formik': patch
---

Revert Yup transform support for the time being, this may be re-introduced in a future release under an opt-in prop.
21 changes: 5 additions & 16 deletions packages/formik/src/Formik.tsx
Expand Up @@ -218,7 +218,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({
*/
const runValidationSchema = React.useCallback(
(values: Values, field?: string): Promise<FormikErrors<Values>> => {
const schema = getValidationSchema(props.validationSchema);
const validationSchema = props.validationSchema;
const schema = isFunction(validationSchema)
? validationSchema(field)
: validationSchema;
const promise =
field && schema.validateAt
? schema.validateAt(field, values)
Expand Down Expand Up @@ -842,11 +845,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
};

const executeSubmit = useEventCallback(() => {
const schema = getValidationSchema(props.validationSchema);
const actualizedValues =
schema && schema.cast ? schema.cast(state.values) : state.values;

return onSubmit(actualizedValues, imperativeMethods);
return onSubmit(state.values, imperativeMethods);
});

const handleReset = useEventCallback(e => {
Expand Down Expand Up @@ -1024,16 +1023,6 @@ export function Formik<
);
}

function getValidationSchema<Values extends FormikValues = FormikValues>(
validationSchema?: FormikConfig<Values>['validationSchema']
) {
if (!validationSchema) {
return;
}

return isFunction(validationSchema) ? validationSchema() : validationSchema;
}

function warnAboutMissingIdentifier({
htmlContent,
documentationAnchorLink,
Expand Down
68 changes: 2 additions & 66 deletions packages/formik/test/Formik.test.tsx
Expand Up @@ -1435,8 +1435,8 @@ describe('<Formik>', () => {
await act(async () => {
try {
await getProps().validateForm();
} catch (err) {
caughtError = (err as Yup.ValidationError).message;
} catch ({ message }) {
caughtError = message;
}
});

Expand All @@ -1450,68 +1450,4 @@ describe('<Formik>', () => {

expect(innerRef.current).toEqual(getProps());
});

it('transforms in yup schema are applied on validation', async () => {
const validationSchema = Yup.object({
users: Yup.array().of(
Yup.object({
firstName: Yup.string()
.transform(currentValue =>
currentValue.split('').reverse().join('')
)
// @ts-expect-error incorrect typing for second arg
.oneOf(['foo'], x => x.value),
})
),
});

const { getProps } = renderFormik({
initialValues: { users: [{ firstName: 'foo' }] },
validationSchema,
});

await act(async () => {
await getProps().validateForm();

expect(getProps().errors).toEqual({
users: [
{
// the transform reverses "foo" to "oof", which then fails the `oneOf` assertion
firstName: 'oof',
},
],
});
});
});

it('transforms in yup schema are applied on submit', async () => {
const validationSchema = Yup.object({
users: Yup.array().of(
Yup.object({
firstName: Yup.string().transform(currentValue =>
currentValue.split('').reverse().join('')
),
})
),
});

const spy = jest.fn();

const { getProps } = renderFormik({
initialValues: { users: [{ firstName: 'foo' }] },
onSubmit: spy,
validationSchema,
});

await act(async () => {
await getProps().submitForm();

expect(spy).toHaveBeenCalledWith(
{
users: [{ firstName: 'oof' }],
},
expect.anything()
);
});
});
});
9 changes: 3 additions & 6 deletions packages/formik/test/withFormik.test.tsx
Expand Up @@ -134,18 +134,15 @@ describe('withFormik()', () => {
});

it('calls validationSchema', async () => {
const validationSchema = Yup.object();

jest.spyOn(validationSchema, 'validate');

const validate = jest.fn(() => Promise.resolve());
const { getProps } = renderWithFormik({
validationSchema,
validationSchema: { validate },
});

act(() => {
getProps().submitForm();
});
await waitFor(() => expect(validationSchema.validate).toHaveBeenCalled());
await waitFor(() => expect(validate).toHaveBeenCalled());
});

it('calls validationSchema function with props', async () => {
Expand Down

1 comment on commit 187e47d

@vercel
Copy link

@vercel vercel bot commented on 187e47d May 31, 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.vercel.app
formik.org
formik-docs-formik.vercel.app
formik-docs-git-main-formik.vercel.app
www.formik.org

Please sign in to comment.