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 values to validationSchema #506

Closed
antonkri97 opened this issue Mar 11, 2018 · 2 comments
Closed

Pass values to validationSchema #506

antonkri97 opened this issue Mar 11, 2018 · 2 comments

Comments

@antonkri97
Copy link

antonkri97 commented Mar 11, 2018

Bug, Feature, or Question?

Feature

Description

It would be nice to have access to the values in the YUP scheme in the validationSchema. Possible use cases: need to validate based on the current values in the form. For example, one numeric value must not exceed another numeric value.

@antonkri97 antonkri97 changed the title Pass values to validationSchema Pass values to validationSchema Mar 11, 2018
@latviancoder
Copy link

latviancoder commented Mar 12, 2018

You can always use validate function and call validateYupSchema function directly with your desired context. Here is an example using synchronous validation and custom context.

validate: (values: FormValues) => {
    const validationSchema = object().shape({
        ...
    });

    try {
        validateYupSchema<FormValues>(values, validationSchema, true, {myCustomContext: 'bla'});
    } catch (err) {
        return yupToFormErrors(err);
    }

    return {};
},

I suggest looking at how Formik itself uses these functions in the source code.

@antonkri97
Copy link
Author

@latviancoder Yes, it can solve my current problem, I would certainly like to have a second FormValues argument in the function validationSchema, but it's also nice

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

No branches or pull requests

2 participants