Zod Discriminated Union Form Router
Model variant form payloads with Zod discriminated unions, routing each discriminant to its own schema, fields, and submit handler.
Code
import { z } from 'zod';
const emailLogin = z.object({
method: z.literal('email'),
email: z.string().email(),
password: z.string().min(8),
remember: z.boolean().default(false),
});
const smsLogin = z.object({
method: z.literal('sms'),
phone: z.string().regex(/^\+\d{7,15}$/),
code: z.string().regex(/^\d{6}$/),
});
const oauthLogin = z.object({
method: z.literal('oauth'),
provider: z.enum(['github', 'google']),
idToken: z.string().min(20),
});
const LoginPayload = z.discriminatedUnion('method', [
emailLogin, smsLogin, oauthLogin,
]);
type LoginPayload = z.infer<typeof LoginPayload>;
const handlers: Record<LoginPayload['method'], (p: never) => string> = {
email: (p) => 'signing in with ' + (p as z.infer<typeof emailLogin>).email,
sms: (p) => 'verifying sms for ' + (p as z.infer<typeof smsLogin>).phone,
oauth: (p) => 'oauth via ' + (p as z.infer<typeof oauthLogin>).provider,
};
function submit(raw: unknown): string {
const parsed = LoginPayload.parse(raw);
return handlers[parsed.method](parsed as never);
}
console.log(submit({ method: 'email', email: 'ada@example.com', password: 'hunter2xx' }));
console.log(submit({ method: 'sms', phone: '+8613800138000', code: '123456' }));Inside the code
Zod Discriminated Union Form Router keeps everything in the function submit.
From zod it pulls z — the imports name exactly which library surface the snippet touches.
What to watch for
- Plan for an install step:
zodis a real dependency, resolved by the package manager.
Using it
The entry point taking unvalidated input is function submit(raw: unknown)
Key statements
const handlers: Record<LoginPayload['method'], (p: never) => string> = {
email: (p) => 'signing in with ' + (p as z.infer<typeof emailLogin>).email,
sms: (p) => 'verifying sms for ' + (p as z.infer<typeof smsLogin>).phone,
oauth: (p) => 'oauth via ' + (p as z.infer<typeof oauthLogin>).provider,
};
return handlers[parsed.method](parsed as never);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- What does this need from the package registry?
- These are real dependencies: `zod`. Without it the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.