#How can I use zod discriminated union for search params?

9 messages · Page 1 of 1 (latest)

hallow cairn
#

I have a list of search params and some of them should not be present if a search param (let's say it's called mode) has a certain enum value. I've tried various things but no success

royal garden
#

what did you try?

hallow cairn
#

I've tried this and variations to this, like using undefined instead of never but I always get ts error either in <Link> components or in loader search params
const schema = z.discriminatedUnion('mode', [
z.object({
mode: z.literal('schedule-appointment'),
slot_id: z.string(),
start_date: z.string().datetime(),
end_date: z.string().datetime(),
appointment_code: z.string(),
zip: z.string().max(10),
}),
z.object({
mode: z.enum(['request-appointment', 'manual-booking']),
slot_id: z.never(),
start_date: z.never(),
end_date: z.never(),
appointment_code: z.never(),
zip: z.string().max(10),
}),
]);

#

If this is not enough I can try to provide an example during the weekend. It's so time consuming to construct an example 😄

royal garden
#

really, why is it so hard to create an example?

#

just fork a stackblitz router example

#

and modify it

hallow cairn
#

Two problems:

  1. If I use z.never type issues at About 2 link
  2. If I use z.undefined type issues in the search middleware in the about route