-
-
Notifications
You must be signed in to change notification settings - Fork 563
Open
Labels
zodZod related issueZod related issue
Description
Add option to use nativeEnum for Zod enums
Current behavior
Currently, generating enums to Zod schemas results in output like this:
export const Currency = zod.union([zod.literal(0), zod.literal(1)]);
export type Currency = zod.input<typeof Currency>;This is useful for validation but not for development, where we would rather use the actual names defined in the source enum.
Generating the same object as a const results in this vastly more useful object for development:
export type Currency = (typeof Currency)[keyof typeof Currency];
export const Currency = {
Chf: 0,
Eur: 1
} as const;Proposed behavior
It would be nice if we could have both at the same time.
Proposed solution
Luckily, Zod allows exactly that:
import { z as zod } from 'zod';
const CurrencyObject = {
Chf: 0,
Eur: 1
} as const;
export const CurrencySchema = zod.nativeEnum(CurrencyObject);
export const Currency = CurrencySchema.enum;
export type Currency = zod.input<typeof CurrencySchema>;This way we can use the schema as CurrencySchema, and the named enum values as i.e. Currency.CHF
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
zodZod related issueZod related issue