zod
zod copied to clipboard
Feature Request: Parse on output schemas
Summary
TL;DR - I'd love to be able to take the output of a zod pipeline (esp. including .transform()
) and have the ability to call .parse()
on it.
Or to think about it a slightly different way, I'd like something like z.infer()
, except a real function I can call .parse()
on instead of just a type utility.
For me, this is going to be most useful when reading previously parsed JSON data back from a JSON column in the database.
Use Case
I'm using zod to parse data I receive from an api, transform it, and then store it in a db using prisma.
On the way in, I can use a schema like this, no problem:
z.object({...}.transform({...}
The problem is reading that data from the db later. If I try to call schema.parse on it, it will fail because the data matches the output schema as opposed to the input schema.
I know about z.infer()
- it does what I want, except it's only a type utility rather than a real schema I can call. Because the data is a JSON column in the db, I'd really prefer to use schema.parse() instead of just typecasting so I can get the runtime safety and not just suppress the compiler errors.
The obvious workaround for now is to track the input and output schemas separately. But then I'm effectively duplicating code between the transform and output schema. Not the end of the world, but not ideal.
I think I'm basically asking to replicate the whole z.input
/ z.output
/ z.infer
paradigm, except with real classes that essentially clone the output of the pipeline for parsing.
Appreciation / Next Steps
Thank you for your consideration and the wonderful library! I've been blown away by how useful everything is.
Also, I could perhaps take a crack on the implementation of this if y'all feel it's a good idea that's worth doing.