Ilmari Kumpula's is a good solution.
Best way to use zod effectively is to follow the logical flow.
OPINION ONLY:
If you are working on a medium+ size project, consider using joi or even writing your own as you go if you have the resources. We have run into many different issues with Zod and have had to implement that many different solutions.
The options we have work, but they get more convoluted the more you add. One of the major ones is having an optional field that can become required, ie, if yes, then now this field is required. There are ways to do it, but not pretty.
We are that far into the project now its going to be a nightmare to change it all, and the testers dont want to regression test everything. We have made around 30 forms so far, with tons of fields, some optional until another option is selected, using refine or superRefine is not very helpful, it doesnt validate until the first parse unless you make sure the error type doesnt return invalid_type. Preprocess works in some cases, discriminated unions in others.
We are purely typescript and this library helps with that, but the effort required in the long run hasnt been worth it.
Some of the outcomes we have found is using literals and discriminated unions can solve a lot of problems you might be scratching your head over. transform and piping also uselful.
We have written a wrapper type function that uses a swiss army knife type field with the options you want to have, ie:
name: asCreate({type: string, conditional: true, conditionalField: otherField, min, max, etc....}),
which then runs through the function an returns the built schema type, using a combo of if else, refine, superrefine, transform, pipe, zod functions, disc unions.
No offense to the authors and contributors, but I feel it needs a rewrite to be more concise and simplified. And the parse don't validate forced opinion has it drawbacks.
Having said all that, our forms are very smart and typesafe, and the zod schemas are the source of truth for the app, it also works perfect as a resolver with react-hook-form, types for TRPC, and keeping dexie indexed db safe too. It just took a ton of unit tests for all of our cases before we got it right.