Require one of two fields
See original GitHub issueSimilar to this Yup issue: https://github.com/jquense/yup/issues/176
I’d like to conditionally validate at least one of n values are set. Yup and Joi solve this problem with a .when
method.
Does Zod currently have a solution to this problem?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:17
- Comments:14 (3 by maintainers)
Top Results From Across the Web
HTML5 required attribute one of two fields - Stack Overflow
This uses the input event on both inputs, and when one is not empty it sets the required property of the other input...
Read more >Require that one of two form fields is filled
Solved: I have two fields on a form, we'll say A and B, and need to require that either A or B is...
Read more >Require User to Fill Out one of two Fields - ServiceNow
Solved: On a Catalog Item that we are placing out on the Service Portal, we have a few fields (among many others): -...
Read more >Making one of two fields required - Jotform
1. Go to Settings - Conditions - Add New - Enable/Require Field · 2. Set up the condition like this: · 3. Set...
Read more >one of two fields required but not both : r/laravel - Reddit
one of two fields required but not both ... I have googled for an hour and haven't found an answer to this. 'price'...
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
Feel the need to comment on this as I keep running into cases this is needed.
I have large schema that needs to require one or another field. A nested partial Zod object with superRefine works for this (as shown above).
What I want to be able to do however, is to do conditional requirement on 1 or 2 fields without making the entire object partial and while having access to all of the fields in the object.
Ex: I have a required enum that has two values: “ValueA” and “ValueB”
Upon “ValueA” then
someOtherFieldA
is required. Upon “ValueB” thensomeOtherFieldB
is required.There are also other required fields within the schema that should remain required without explicitly checking them in something like
superRefine
.Is there any way to do this? Any plans for something like
.when
in Yup?One month! Faster mtf!