zod-funnel — accept many input shapes, parse to one canonical schema #6474
Roman86
started this conversation in
Show and tell
Replies: 1 comment
|
I've prepared the ecosystem page entry as a one-line addition to the Zod Utilities section, but PR creation on this repo seems to be limited to collaborators, so linking the ready branch instead: main...Roman86:zod:ecosystem-zod-funnel (single commit on top of current |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi! I built a small utility on top of Zod 4 and would love to share it with the ecosystem.
The problem. APIs evolve: a field gets renamed, an old client still sends the legacy payload, a third party uses its own naming. Plain Zod can express this — an
.or(...).transform(...).pipe(...)chain — but that's a fairly advanced corner of the API: not every Zod user knows to reach for it, the target schema has to be repeated in every branch, and forgetting the final.pipe(target)re-validation silently leaks unvalidated data. In practice people fall back to ad-hoc normalizers scattered around the codebase.zod-funnelpackages that pattern into one small high-level primitive, so the correct wiring is the only wiring you can write.The idea. Declare the canonical schema once, then funnel each alternate shape into it with a small, type-checked mapper:
PersonFlexis a real Zod schema —parse,safeParse,.array(),.optional()and the rest of the API work as usual. Sources don't have to be objects: a bare string, number, or tuple works too.Design notes:
z.union([target, ...mapped]), nothing custom at parse time..or()chain).Links: GitHub · npm
I'd be happy to add it to the ecosystem page — PR incoming, will link it here.
Feedback very welcome — naming, semantics, edge cases I've missed.
All reactions