Pass component props as context for yup validation
See original GitHub issueFeature
Yup validation can accept the context which can be used for validation purposes, but which is not a part of schema/values. Can we pass props as context?
Current Behavior
I see that validateYupSchema can accept context argument but it’s not used anywhere.
Desired Behavior
Be able to use component’s props and validation rules.
Suggested Solutions
Add component’s props as context for validateYupSchema()
Issue Analytics
- State:
- Created 6 years ago
- Comments:19 (8 by maintainers)
Top Results From Across the Web
How to use Yup context variables in Formik validation
The validation library Yup allows you to pass in values, which aren't validated themselves, to aid in validation using a context. This is...
Read more >How to access context inside Yup schema - Stack Overflow
I'm trying to create dynamic Yup schema According to my component state. For example: import React, { useContext } from "react"; import { ......
Read more >Formik - Advanced Yup Validation with Context - CodeSandbox
This is an example of a simple email validation form with React.js and Formik's HoC using a custom synchronous validator using yup and...
Read more >Formik validations with contexts | DailyCoding
Everything, what we have to do is to pass field name as a props. It will check if form has any error on...
Read more >Advanced Usage - React Hook Form
With the Form component injecting react-hook-form 's props into the child component, ... Use the custom hook, by passing the validation schema.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Rather than passing the whole set of component props, it seems simple enough to add another prop
validationContext
that would ultimately be passed to the yup context options. I know this was closed automatically. But, would you be open to a PR that adds another propvalidationContext
?My PR was closed for a fix to this issue for some valid reasons. Although Formik is somewhat “inspired” by Yup, the two aren’t married. There are no props that are “just for Yup’s sake”. You can pass a Yup schema, or any other schema with a
.validate(values)
method. Those other schemas however may not accept a second argumentoptions.context
. This means something likevalidationContext
would be a prop “just for Yup’s sake”.Similar to @klis87 solution above and another referenced here I think a good solution is to just use React composability to solve this
See comments on PR for some more info: https://github.com/jaredpalmer/formik/pull/1508
I think the real solve for this is some documentation.