Zod Discriminated Union Form Router

Model variant form payloads with Zod discriminated unions, routing each discriminant to its own schema, fields, and submit handler.

TypescriptForm Validation

Code

typescriptsnippet
40 lines
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: zod is 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

FAQ

What does this need from the package registry?
These are real dependencies: `zod`. Without it the snippet will not load.