How to conditionally validate at least one of n values are set?
See original GitHub issueI have 5 form fields, at last one of which must be populated for validation to succeed. I attempted this, but soon ran into cyclic reference issues:
tag_business: yup.string().when(['tag_cost_centre', 'tag_project_code', 'tag_internal_order', 'tag_business'], {
is: (tag_cost_centre, tag_project_code, tag_internal_order, tag_business) => {
return !(tag_cost_centre, tag_project_code, tag_internal_order, tag_business)
},
then: yup.string().required()
})
What would be the best way to implement this?
Issue Analytics
- State:
- Created 6 years ago
- Comments:28 (3 by maintainers)
Top Results From Across the Web
Yup - How to conditionally validate at least one of n values are ...
You can this: a: yup.string().when(['b', 'c', 'd'], { is: (...fields) => fields.some((field) => field === true), then: yup.string().required() }) ...
Read more >yup - How to conditionally validate at least one of n ... - RunKit
1. var yup = require('yup') ; 2. var invariant = require('invariant') ; 3. var without = require("lodash.without") ; 4. ; 5. yup.addMethod(yup.object,...
Read more >How to conditionally validate at least one of n values are set ...
All you need to do is list all the pair-wise (2-tuples) combinations. As you have 4 fields, you expect to have 6 pairs...
Read more >How to Create an Optional Dynamic Validation Schema based ...
In this lesson we'll show how to setup a nested validation structure using the `yup` library. We'll then use the `yup.lazy` method to...
Read more >Yup conditional validation - CodeSandbox
This is an example how to build form validations for fields that only exist based on the value of another field. validation. formik....
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 FreeTop 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
Top GitHub Comments
instead of using examples above, i just created a ‘ghost’ field in yup validation schema that checks specified fields, alot less code and no cyclic errors
This will generate an error and prevent submit.
When using Formik Fields just remember to include additional errors from this ‘ghost’ field, like:
(errors as any)
because TypeScript will not see this error (field is not in initialValues)