Is there a way in Zod to validate a field in a schema conditionally on another fields value?
i.e if z.object.itemType is set then z.object.itemMeta should not be null.
Using refine it appears you don't have access to the object your inside of, so you can't check there and I don't want to refine on the whole object as I can only return one error message.
Any thoughts or ideas?
3 Replies
You can use the the superRefine method it will give you more control https://github.com/colinhacks/zod#superrefine
GitHub
GitHub - colinhacks/zod: TypeScript-first schema validation with st...
TypeScript-first schema validation with static type inference - GitHub - colinhacks/zod: TypeScript-first schema validation with static type inference
I thought this wouldn't work how I wanted when I first saw it, but you are right! Thank you!
I like using unions or discriminated unions in some cases like this, really depends