z.enum for as const numbers?
See original GitHub issueThis works nicely:
const VALUES = ["Salmon", "Tuna", "Trout"] as const;
const FishEnum = z.enum(VALUES);
// 'Salmon' | 'Tuna' | 'Trout'
But this does not work:
const VALUES = [1, 2, 3] as const;
const OrderEnum = z.enum(VALUES);
// 1 | 2 | 3
This is due to the requirement of having an array of strings, although a union of numbers work.
Thoughts?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Handbook - Enums - TypeScript
The enum member is initialized with a constant enum expression. A constant enum expression is a subset of TypeScript expressions that can be...
Read more >Creating a zod enum from an object - typescript - Stack Overflow
According to the zod documentation, i can do this: const properties = [ { value: "entire_place", label: "The entire place" } ...
Read more >Enum validation support? · Issue #8 · colinhacks/zod - GitHub
I will definitely consider using a syntax like the one you recommended at some point though. Current usage: const MyEnum = z.enum([ z.literal('Bar'),...
Read more >Untitled
nativeEnum ()`, which lets you create z Zod schema from an existing TypeScript `enum`: ... ```ts const Location = z.object({ latitude: z.number(), ...
Read more >TypeScript enums: How do they work? What can they be used ...
Constant enum members # · Number literals or string literals · A reference to a previously defined constant enum member (in the current...
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
Enums should never be numbers. By default enum values in TypeScript are numbers, but this is a footgun and an antipattern.
If you add another color the actual under-the-hood value changes:
Which is the source of a lot of bugs and confusion. So Zod forces you to use strings.
This also lets you access the elements of the enum like so:
Which is importany, and wouldn’t be possible with numbers.
I recommend writing a generic function that accepts a list of numbers and exports a
z.custom
schema:@colinhacks this feature would increase the parity with the native TypeScript enum. Also would allow validation of a specific set of numbers.
I know it’s possible to achieve the same validation using
union
, but the error is not as descriptive.Numbers seem to be already supported, so it’s a matter of adjusting the types:
https://github.com/colinhacks/zod/blob/ab75f4dad2a6738c019fe4edea6c8603c25ecb18/src/types.ts#L2940-L3012
For those looking for a dirty solution until this be addressed, it’s possible to do the following: