Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Pass Yup context to validationSchema using new prop validationSchemaContext in <Formik> #3852

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

john-raymon
Copy link

@john-raymon john-raymon commented Aug 1, 2023

Closes #3450

  • This PR creates a new prop in the <Formik> component called validationSchemaContext for Yup context, which is then passed to the validationSchema here (see below):

const runValidationSchema = React.useCallback(
(values: Values, field?: string): Promise<FormikErrors<Values>> => {
const validationSchema = props.validationSchema;
const schema = isFunction(validationSchema)
? validationSchema(field)
: validationSchema;
const promise =
field && schema.validateAt
? schema.validateAt(field, values, {
context: props.validationSchemaContext
})
: validateYupSchema(values, schema, false, props.validationSchemaContext);

Note the validateYupSchema function already had an optional context argument, and it also already had a third argument called sync which is set to default to false in the function definition. , no changes were made below. (see below)

export function validateYupSchema<T extends FormikValues>(
values: T,
schema: any,
sync: boolean = false,
context?: any
): Promise<Partial<T>> {
const normalizedValues: FormikValues = prepareDataForValidation(values);
return schema[sync ? 'validateSync' : 'validate'](normalizedValues, {
abortEarly: false,
context: context || normalizedValues,
});
}

  • Wrote tests in both Formik.test.tsx and in yupHelpers.test.ts.

  • Added types for the new prop.

  • Updated the docs to include the new prop in the <Formik> api page

@changeset-bot
Copy link

changeset-bot bot commented Aug 1, 2023

⚠️ No Changeset found

Latest commit: 2ef7ef6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Aug 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
formik-docs ⬜️ Ignored (Inspect) Visit Preview Aug 1, 2023 5:58am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 1, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2ef7ef6:

Sandbox Source
Formik TypeScript Playground (forked) Configuration

@mikegreiling
Copy link

What can be done to move this PR forward? I could really use this feature and I'd be happy to contribute, but it looks like this has gone 8 months with no feedback from code owners :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pass context to yup validation schema
2 participants