#Zod conditional fields

1 messages · Page 1 of 1 (latest)

hot zinc
#

To summarize I am unable to find a way to make certain fields required / optional depending upon intention in a single action function.

For eg:
For create intent: name and address should be required. id should be optional.

For update intent: name , id and address should all be required.

Any ideas for how to achieve the same?

tired mantle
#

You'd probably need two different Zod schemas, where some of the fields are marked with .optional() right?

stray dew
#

Two different schema with two différents actions?

narrow crest
#

You could do two different schemas. Or you can make a base schema for all the required fields and then conditionally merge schema for specific use cases.