Zod 3 `transform` breaking change
See original GitHub issueIn version 2 we utilised zod.transform
method to transform an object into an array, and validated each array item. Using zod.array
directly resulted in parse error ‘Expected an array, received object’.
Background: in Ember Data hasMany
relations are essentially objects, but semantically they represent collections: they treat themselves as array-like structures with implementation of map
, filter
and others, and capable of returning a true array via toArray
method.
Version 2 code:
interface SyncHasMany<T> {
// ...
toArray(): T[];
}
function hasMany<T extends zod.ZodTypeAny>(itemSchema: T): zod.ZodTransformer<zod.ZodUnknown, zod.ZodArray<T>> {
return zod.unknown().transform(
zod.array(itemSchema),
// In case it is hasMany we should convert it to plain Array so Zod can digest it.
rel => Array.isArray(rel) ? rel as zod.TypeOf<T>[] : (rel as SyncHasMany<zod.TypeOf<T>>).toArray(),
);
}
But in ver. 3 this approach is no longer available. The question is how can I achieve the same result now? I want zod to validate a hasMany
relation using zod.array(itemSchema)
schema as if the content of this relation was a plain array.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
/README.md | zod@v3-snapshot-2021-01-21 | Deno
Breaking changes in v3. The minimum TypeScript version has increased from 3.7 to 4.1. Several features have been rewritten to use recursive conditional...
Read more >zod - npm
Transformers! But better! See the "breaking changes" section to understand the syntax changes. You can now import Zod like import { z }...
Read more >@conform-to/dom - NPM Package Versions - Socket - Socket.dev
What's Changed. Breaking Changes. Conform has undergone a massive redesign in its validation mechanism. This includes replacing some of the high level ...
Read more >Validating PostgreSQL results & inferring query static types
If there is a breaking change, the application fails with a loud error that is ... Using zod transform you can refine the...
Read more >Dru-Zod - Krypton Wiki - Fandom
—Dru-Zod General Dru-Zod is a member of the House of Zod and comes from the future. ... Dru takes this as proof that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@colinhacks
zod.preprocess
works great, thank you very much!Excellent!