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 2 beta 5: applying transform() to arrays

See original GitHub issue

Hi,

I’m trying to use .transform() on a string array to remove possible duplicates. This is my schema:

DummyInput = z.object({
  pokemonTypes:
    z.array(string())
    .nonempty()
    .transform(cleanDuplicateStringsFromArray)
})

The function signature is (array: string[]) => string[]. Seems right - take an array of strings and transform it to itself… although I get an error:

TS2345: Argument of type '(array: string[]) => string[]' is not assignable to parameter of type '(arg: [string, ...string[]]) => string | Promise<string>'.   Type 'string[]' is not assignable to type 'string | Promise<string>'.     Type 'string[]' is not assignable to type 'string'.

Am I missing something here?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tmtroncommented, Sep 24, 2020

Sice you call .nonempty(), your input type is not just a string[], but the tuple [string, ...string[]].

Per default, the transformer function must also return the same type (but you could override it), so your transformer function could be like this:

function cleanDuplicateStringsFromNonemptyArray(arg: [string, ...string[]]) {
  // get an array of distinct items
  const distinctItems = [...new Set(arg)];
  // destructure array to match output type
  const [first, ...rest] = distinctItems;
  return [first, ...rest];
}
0reactions
colinhackscommented, Sep 27, 2020

Thanks @tmtron!

@grreeenn there probably shouldn’t be a difference but I guess it worked out in your case. If anyone else is unhappy with the typing of .nonempty feel free to implement your own validation logic with .refine (which never changes the inferred type).

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC: Refactoring transformers and Zod 3 · Issue #264 - GitHub
This converts the string "5" to the number 5 . This is then passed into the transformation function. But wait: val is now...
Read more >
zod - npm
TypeScript-first schema declaration and validation library with static type inference. Latest version: 3.20.2, last published: a day ago.
Read more >
Zod NPM | npm.io
z.enum is a Zod-native way to declare a schema with a fixed set of allowable string values. Pass the array of values directly...
Read more >
Why Zod 2 isn't leaving beta - Colin McDonnell
The reason Zod 2 has been in beta for so long (well, ... Transformers are a mechanism within Zod to convert data from...
Read more >
How to transform object to array before parsing in Zod
I didn't test that, but seems like should work: const FieldsSchema = z.object({ fullName: z.string(), type: z.string() }); export const ...
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