zod icon indicating copy to clipboard operation
zod copied to clipboard

Is it possible to create a zod enum from a string literal type

Open T04435 opened this issue 1 month ago • 1 comments

What zod fn should I use to make the BarSchema valid?

type Foo = 'foo' | 'bar'; // generated

interface Bar {
 foo: Foo,
 id: string,
}

export const BarSchema = z.object({
  foo: z.enum(Foo), // what should this be?
  id: z.string()
}) satisfies z.ZodSchema<Bar>;

I know I can redefine the Foo type like:

foo: z.enum(['foo', 'bar'])

But the point is not to redefine the generated types.

T04435 avatar May 27 '24 12:05 T04435