I have the following object that is autogenerated
export const ReportDimensions = {
CHANNELS: 'CHANNELS',
DAY: 'DAY',
DOW: 'DOW',
MONTH: 'MONTH',
WEEK: 'WEEK'
} as const;
I would like to use zod, and allow only value that are in this object, so like
["MONTH","WEEK","DOW", "DAY", "CHANNELS"]
I tried to do
z.enum(Object.keys(ReportDimensions))
but I get
No overload matches this call. Overload 1 of 2, '(values: readonly [string, ...string[]], params?: RawCreateParams): ZodEnum<[string, ...string[]]>', gave the following error. Argument of type 'string[]' is not assignable to parameter of type 'readonly [string, ...string[]]'. Source provides no match for required element at position 0 in target. Overload 2 of 2, '(values: [string, ...string[]], params?: RawCreateParams): ZodEnum<[string, ...string[]]>', gave the following error. Argument of type 'string[]' is not assignable to parameter of type '[string, ...string[]]'. Source provides no match for required element at position 0 in target.ts(2769)
How can I properly do this ?
CodePudding user response:
- Try using
z.nativeEnum(ReportDimensions)
OR
- Try changing
const ReportDimensions = { ... } as const
toconst ReportDimensions = [ 'CHANNELS', 'DAY', 'DOW', 'MONTH', 'WEEK' ] as const;
.