Zod 2 beta 5: applying transform() to arrays
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >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
Sice you call
.nonempty()
, your input type is not just astring[]
, 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:
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).