-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Similar to this Yup issue: jquense/yup#176 and creating a new issue out of #61.
Many users of zod would like to do conditional requirement or validation of fields based on either fields passed as context to zod, or based off of the value of another schema property. This is common to do in form validations.
The closed issue #61 addresses how to do conditional validation with superRefine. This works well enough in small cases, but many folks have large complex schemas and we would like to keep the existing validation on an object i.e. not have to make the entire object partial in order for conditional requirement to work.
This issue stems from the reaction to my comment #61 (comment)
Pasting from the comment as my case is the same:
I have large schema that needs to require one or another field. A nested partial Zod object with superRefine works for this.
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.
The reason I chose zod over yup was TypeScript support, but in yup's beta versions TS support has improved a lot. Not having a functionality like this is a big blocker for me wanting to keep using zod. Thanks!