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.

z.enum for as const numbers?

See original GitHub issue

This 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
colinhackscommented, Sep 17, 2021

Enums should never be numbers. By default enum values in TypeScript are numbers, but this is a footgun and an antipattern.

enum Color { Red, Green };
Color.Red; // 0

If you add another color the actual under-the-hood value changes:

enum Color { Blue, Red, Green };
Color.Red; // 1

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:

const Color = z.enum(["Red", "Green"]);
Color.enum.Red; // "Red"

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:

const numberEnum = <Num extends number, T extends Readonly<Num[]>>(
  args: T
): z.ZodSchema<T[number]> => {
  return z.custom<T[number]>((val: any) => args.includes(val));
};

const nums = [1, 2, 3] as const;
const schema = numberEnum(nums);
1reaction
diogoazevedoscommented, Mar 14, 2022

@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:

const schema = z.nativeEnum({
  a: 100,
  b: 200,
  c: 300
} as const);
Read more comments on GitHub >

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

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