Codec encode types and restrictions
#5385
Replies: 1 comment
-
|
You're correct that Transforms in Zod are unidirectional: if a schema uses If you need to support a wider range of input types for encode, you may need to adjust your schemas or provide a custom encode function that ensures the output always matches the expected type for schema A. If you have further questions or want to discuss workarounds for specific cases, let me know! If this answers your question, feel free to close the issue. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My assumption is that
is effectively implemented as
(with obvious caveats around
safe,async, object wrappers, and any internal optimizations).If this is the case, it leads to 2 questions:
the types here imply that decoding is performed as described above, while encoding is a direct pass-through of the provided function.
https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/classic/schemas.ts#L1927-L1928
Here is a simple example that I expect to work, but fails type-checking because encode must return the output of A (i.e., string), rather than the input.
I assume this has to do with nested schemas, but the typing error in particular seems quite odd, especially when all of my runtime testing and repo spelunking suggests the behavior described above.
Beta Was this translation helpful? Give feedback.
All reactions