Z
Zod14mo ago
Kas.st

Kas.st - Hi everyone, I have a schema with an e...

Hi everyone, I have a schema with an enum:
const schema = z.object({
foo: z.enum(["foo", "bar", "baz"])
})
const schema = z.object({
foo: z.enum(["foo", "bar", "baz"])
})
how can I get all the valid values from the enum? I saw that theres a .options() method on z.enum but I dont know how I'll get to that if I only have the schema?
Solution:
```ts // Type level type EnumValue = typeof schema["shape"]["foo"]["options"][number]; // Runtime (with types also)...
Jump to solution
1 Reply
Solution
Sikari
Sikari14mo ago
// Type level
type EnumValue = typeof schema["shape"]["foo"]["options"][number];

// Runtime (with types also)
const enumValues = schema.shape.foo.options;
// Type level
type EnumValue = typeof schema["shape"]["foo"]["options"][number];

// Runtime (with types also)
const enumValues = schema.shape.foo.options;
Want results from more Discord servers?
Add your server