Use Zod schema after parse & transform as a new schema
I am looking how I could handle from validation with one schema so it can be used on the ReactHookForm zod resolver and tRPC input validation.
lets say my form accepts two values
schema
is a parser for client form input which I would like to use resulting type of that schema as a new schema for tRPC input validation. Can I do this somehow? Or my bet is just don't do the transform and use that schema both on client and backend and on backend extract needed fields and do further processing. This will be simpler but unnecessary bloat of data payload would be then sent to the server4 Replies
So just going to add a clarifying question, you are using the schema:
for the client side form validation? and for example you would want the tRPC validation schema to look something like this?:
Is that correct or do i have it wrong?
@Jacob yes, exactly.
@Mugetsu
So what i would recommend is the following:
What you could also do, is lets say we have many client fields that aren't all needed on the server e.g:
Or instead of omitting the value you could use the
.pick
method:
Note: When using the .extend
method even if you remove the field on the clientSchema
it will still be there on the serverSchema
Thanks @Jacob it helped me a lot with ur suggestions!