question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Zod 3 `transform` breaking change

See original GitHub issue

In 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
azhivcommented, Sep 2, 2021

@colinhacks zod.preprocess works great, thank you very much!

0reactions
colinhackscommented, Sep 3, 2021

Excellent!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found