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

Pass context to yup validation schema #3450

Open
TaylorDale opened this issue Jan 6, 2022 · 3 comments · May be fixed by #3852
Open

Pass context to yup validation schema #3450

TaylorDale opened this issue Jan 6, 2022 · 3 comments · May be fixed by #3852

Comments

@TaylorDale
Copy link

Feature request

Be able to pass a context object to yup in conjunction with validationSchema

Current Behavior

Context is undefined when it is passed to Yup for validation.

Desired Behavior

Able to pass in a context object to the Formik form that will be sent when validationSchema is called

Suggested Solution

Add a validationContext attribute

Describe alternatives you've considered

This may be possible with:

validate={async (values) => {
        await schemaShape.validate(values, {
          abortEarly: false,
          context: { val1, val2 },
        });
      }}

However, this does not help when validating for example, blur events. Instead an error is caused as the context is undefined and my validation relies on context.

@JameelKhan9
Copy link

Faced the same blocker, as this is a prerequisite for attempting multi schema resolvers.

@martinharyanto
Copy link

Yeah it would be nice if it is possible to send a parameter to validationSchema. It is a bit weird knowing this is not default behavior for on yup. I am also stuck, and when() is not what I am looking for since I want to be able to control what to validate based on variable outside of the form.

@ar4hc
Copy link

ar4hc commented Oct 27, 2022

Workaround here

Overrides the withFormik HOC.

Doesn't help if you're not using withFormik but <Formik />, though...

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