question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Require one of two fields

See original GitHub issue

Similar 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:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

66reactions
ckifercommented, Jul 1, 2022

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” then someOtherFieldB 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?

36reactions
DrBronsycommented, Aug 1, 2022

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” then someOtherFieldB 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found