Skip to content

Commit

Permalink
pass Yup context to validationSchema using new prop validationSchemaC…
Browse files Browse the repository at this point in the history
…ontext
  • Loading branch information
john-raymon committed Aug 1, 2023
1 parent b35b9ba commit ccdcb39
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({
: validationSchema;
const promise =
field && schema.validateAt
? schema.validateAt(field, values)
: validateYupSchema(values, schema);
? schema.validateAt(field, values, {
context: props.validationSchemaContext
})
: validateYupSchema(values, schema, false, props.validationSchemaContext);
return new Promise((resolve, reject) => {
promise.then(
() => {
Expand Down
6 changes: 6 additions & 0 deletions packages/formik/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ export interface FormikConfig<Values> extends FormikSharedConfig {
values: Values,
formikHelpers: FormikHelpers<Values>
) => void | Promise<any>;

/**
* A context object to be passed to the Yup schema's `validate` method.
*/
validationSchemaContext?: any;

/**
* A Yup Schema or a function that returns a Yup schema
*/
Expand Down
24 changes: 24 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1454,4 +1454,28 @@ describe('<Formik>', () => {

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

it('should pass context to validationSchema', async () => {
const validationSchema = Yup.object().shape({
usernameOrEmail: Yup.string().when(
'$validateAsEmail',
(validateAsEmail, schema) =>
validateAsEmail ? schema.email('Invalid email') : schema
),
});

const { getProps } = renderFormik({
initialValues: { usernameOrEmail: 'john' },
validationSchema,
validationSchemaContext: { validateAsEmail: true },
});

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

expect(getProps().errors).toEqual({
usernameOrEmail: 'Invalid email',
});
});
});
18 changes: 18 additions & 0 deletions packages/formik/test/yupHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const deepNestedSchema = Yup.object({
}),
});

const whenContextSchema = Yup.object().shape({
name: Yup.string().when('$isNameRequired', (isNameRequired, schema) => {
return isNameRequired ? schema.required('name is required') : schema;
}),
});

describe('Yup helpers', () => {
describe('yupToFormErrors()', () => {
it('should transform Yup ValidationErrors into an object', async () => {
Expand Down Expand Up @@ -109,5 +115,17 @@ describe('Yup helpers', () => {
]);
}
});

it('should provide context values as context to when method', async () => {
try {
await validateYupSchema({ name: '' }, whenContextSchema, false, {
isNameRequired: true,
});
} catch (e) {
const err = e as Yup.ValidationError;
expect(err.name).toEqual('ValidationError');
expect(err.errors).toEqual(['name is required']);
}
});
});
});

0 comments on commit ccdcb39

Please sign in to comment.