Skip to content
Sarj Code Standards

require-branded-id-schema

Brand Zod identifier schemas so validated identifiers retain their domain.

Why

Validating the format of a scenario ID or session ID still produces a plain string. Distinct brands let TypeScript catch swaps between roles that use the same primitive representation.

Fix

Apply a distinct Zod brand to each identifier domain at its parse boundary. Derive the TypeScript ID type with z.output or z.infer, and reuse its schema in objects that carry the ID.

Examples

Before — flagged A take and a session validate as the same type
src/recording.ts
import { z } from "zod";
export const RecordingSchema = z.strictObject({
takeId: z.uuid(),
sessionId: z.uuid(),
});
After — preferred Brand each parsed identifier with its own domain
src/recording.ts
import { z } from "zod";
const TakeIdSchema = z.uuid().brand<"TakeId">();
const SessionIdSchema = z.uuid().brand<"SessionId">();
export const RecordingSchema = z.strictObject({
takeId: TakeIdSchema,
sessionId: SessionIdSchema,
});