Complex nested Codecs #5295
Replies: 1 comment 2 replies
-
You're on the right track—Zod codecs are fully composable and can be nested inside objects, arrays, and other schemas, with no restrictions on where you use them. When you call For unions/discriminated unions, the recommended pattern is to keep your encode/decode logic DRY by reusing the encode/decode functions from your inner codecs. You can also compose smaller codecs using intersections ( So, your approach is correct given the current API. For deeply nested structures, helper functions and careful manual wiring are the way to go. If you want to keep things DRY, consider extracting encode/decode helpers and using intersections for composition. If you hit any runtime issues with discriminated unions, it's usually because the underlying shape is transformed by codec logic in unexpected ways—so double-check your encode/decode paths. If you have a runnable reproduction or hit a specific edge case, feel free to share it—@colinhacks will see it and can weigh in. If this answers your question, please 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.
-
Hello! I tried asking on Discord as well, but not everybody is there, and I want to increase my chances of getting help 🙂
I found a somewhat similar discussion, but the goal seems different than what I am trying to achieve.
I am currently investigating if I could use the new codecs to transform a complex data structure from our server to our client. As this new feature has a few examples in the documentation and is even cited as "very useful when parsing data at a network boundary", I assume this is the way to go.
What is not in the documentation or examples though, is how to handle codecs that are nested into one-another, and this is where my issues start.
Assuming I have 2 types of elements, that have slightly different shape between my server and my client, I would create codecs like this. I will try to simplify as much as possible for this discussion:
So far so good.
What get's tricky for me to understand though, is how to define the parent schemas based on this.
This is, purely in types, what I need to deal with:
Instinctively, the way I would replicate it with Zod is by using
Content
this way:but then the function for
decode
andencode
start to feel off.From what I understood, all the schemas in Zod are processed from the lower levels to the upper levels. But this leads to the data arriving to the codec as partially transformed, and in order to satisfy both TS and the runtime, I end up having to re-transform the data so that it can be handled by the "upper" codec, like this:
and it quickly gets worse as I add more nesting 😕
I found some kind of alternative like this, but it still feels wrong, because it works only if I use
out
, but not if I also usein
:and I suspect that the data ends up not being transformed correctly at all the levels.
So my question is basically, is there a better way of doing this, or am I abusing the feature to do something it was not meant to do?
The documentation says that codecs are like any other schema, and there are no rules on where I can use them, so I assume it's more of a skill issue than a feature limitation 😅
I hope somebody with a better understanding of codecs can help me figure it out 🤞🏻
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions