T
TanStack•10mo ago
adverse-sapphire

How can I use zod discriminated union for search params?

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
5 Replies
sensitive-blue
sensitive-blue•10mo ago
what did you try?
adverse-sapphire
adverse-sapphireOP•10mo ago
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 😄
sensitive-blue
sensitive-blue•10mo ago
really, why is it so hard to create an example? just fork a stackblitz router example and modify it
adverse-sapphire
adverse-sapphireOP•10mo ago
Here it is https://stackblitz.com/edit/tanstack-router-qm8eay?file=src%2Froutes%2F__root.tsx,src%2Froutes%2Fabout.tsx&preset=node 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
sensitive-blue
sensitive-blue•10mo ago
can you please create a gh issue for this?

Did you find this page helpful?