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
prakhar_goel
prakhar_goel2y ago
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
c1ub
c1ubOP2y ago
I thought this wouldn't work how I wanted when I first saw it, but you are right! Thank you!
gymnocarpa
gymnocarpa2y ago
I like using unions or discriminated unions in some cases like this, really depends
Want results from more Discord servers?
Add your server