| 3065 | ///////////////////////////////////////////////////// |
| 3066 | |
| 3067 | const getDiscriminator = <T extends ZodTypeAny>(type: T): Primitive[] => { |
| 3068 | if (type instanceof ZodLazy) { |
| 3069 | return getDiscriminator(type.schema); |
| 3070 | } else if (type instanceof ZodEffects) { |
| 3071 | return getDiscriminator(type.innerType()); |
| 3072 | } else if (type instanceof ZodLiteral) { |
| 3073 | return [type.value]; |
| 3074 | } else if (type instanceof ZodEnum) { |
| 3075 | return type.options; |
| 3076 | } else if (type instanceof ZodNativeEnum) { |
| 3077 | // eslint-disable-next-line ban/ban |
| 3078 | return util.objectValues(type.enum); |
| 3079 | } else if (type instanceof ZodDefault) { |
| 3080 | return getDiscriminator(type._def.innerType); |
| 3081 | } else if (type instanceof ZodUndefined) { |
| 3082 | return [undefined]; |
| 3083 | } else if (type instanceof ZodNull) { |
| 3084 | return [null]; |
| 3085 | } else if (type instanceof ZodOptional) { |
| 3086 | return [undefined, ...getDiscriminator(type.unwrap())]; |
| 3087 | } else if (type instanceof ZodNullable) { |
| 3088 | return [null, ...getDiscriminator(type.unwrap())]; |
| 3089 | } else if (type instanceof ZodBranded) { |
| 3090 | return getDiscriminator(type.unwrap()); |
| 3091 | } else if (type instanceof ZodReadonly) { |
| 3092 | return getDiscriminator(type.unwrap()); |
| 3093 | } else if (type instanceof ZodCatch) { |
| 3094 | return getDiscriminator(type._def.innerType); |
| 3095 | } else { |
| 3096 | return []; |
| 3097 | } |
| 3098 | }; |
| 3099 | |
| 3100 | export type ZodDiscriminatedUnionOption<Discriminator extends string> = ZodObject< |
| 3101 | { [key in Discriminator]: ZodTypeAny } & ZodRawShape, |