How do I map an array of values into discriminated union options #985
-
I'd like to be able to map an array of string values into discriminated union options and use them with const validationSchema = z.discriminatedUnion("taxClassification", Object.values(TaxClassification).map(taxClassification => {
return z
.object({
taxClassification: z.literal(taxClassification),
exemptPayeeCode:
taxClassificationExemptPayeeCodeSchemaMap[taxClassification],
exemptFatcaCode:
taxClassificationExemptFatcaCodeSchemaMap[taxClassification],
})
}); I can't though because "Source provides no match for required element at position 0 in target." I would typically try to type my mapped array as |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I have played around with this and I don't really see a good solution without changing zod's code. Even if I manually export |
Beta Was this translation helpful? Give feedback.
I have played around with this and I don't really see a good solution without changing zod's code. Even if I manually export
ZodDiscriminatedUnionOption
, I'm still running into problems. Would you like to make this into an issue? In my opinion, this is a bug. I think that you should be able to do what you are trying to do.