Z
Zod9mo ago
Gludek

Gludek - Heey, can I somehow extend/merge discr...

Heey, can I somehow extend/merge discriminated union with object? my union
z
.discriminatedUnion('ssnMode', [
z.object({
ssnMode: z.literal('true'),
ssn: z.string(),
}),
z.object({
ssnMode: z.literal('false'),
passport: z.string(),
dob: z.date(),
}),
])
z
.discriminatedUnion('ssnMode', [
z.object({
ssnMode: z.literal('true'),
ssn: z.string(),
}),
z.object({
ssnMode: z.literal('false'),
passport: z.string(),
dob: z.date(),
}),
])
and object
z.object({
adult: z.boolean(),
patientName: z.string(),
patientSurname: z.string(),
patientPhone: z.string(),
patientEmail: z.string().optional(),
})
z.object({
adult: z.boolean(),
patientName: z.string(),
patientSurname: z.string(),
patientPhone: z.string(),
patientEmail: z.string().optional(),
})
Solution:
Not currently possible. There are work arounds though, but it requires you to redefine the union every time.
Jump to solution
3 Replies
Gludek
GludekOP9mo ago
I can't use .and becasue I need to extend it later to add fields based on this object
Solution
Scott Trinh
Scott Trinh9mo ago
Not currently possible. There are work arounds though, but it requires you to redefine the union every time.
Gludek
GludekOP9mo ago
oh well, then I'll probably nest the union then

Did you find this page helpful?