Designs Zod schemas following Zod-first development. Creates validation schemas, branded types, discriminated unions, and transforms. Infers TypeScript types from schemas...
Guide the design and implementation of Zod schemas following the project's Zod-first development approach, where schemas are defined before implementing business logic.
z.infer<> instead of manual type definitions| Need | Pattern | Example |
|---|---|---|
| Basic validation | Primitives | z.string().min(1) |
| Domain safety | Branded types | transform((v) => v as EntitySlug) |
| Multiple types | Discriminated union | z.discriminatedUnion('type', [...]) |
| Cross-field rules | Refinement | .refine((data) => ...) |
| Data normalization | Transform | .transform((v) => v.trim()) |
| Partial updates | Partial schema | Schema.partial() |
import { z } from 'zod';
// Slug-based entity (Categories, Products, Channels, etc.)
const SlugSchema = z.string().regex(/^[a-z0-9-]+$/);
// Name-based entity (ProductTypes, Attributes, TaxClasses, etc.)
const NameSchema = z.string().min(1).max(100).trim();
// Branded types for domain safety
const EntitySlugSchema = SlugSchema.transform((v) => v as EntitySlug);
// Standard entity shape
const EntitySchema = z.object({
name: z.string().min(1),
slug: z.string().regex(/^[a-z0-9-]+$/),
description: z.string().optional(),
});
// Always infer types from schemas
type Entity = z.infer<typeof EntitySchema>;
For detailed patterns and examples, see:
src/modules/config/schema/
āāā schema.ts # Main configuration schema
āāā primitives.ts # Reusable primitive schemas
āāā entities/ # Entity-specific schemas
ā āāā category.ts
ā āāā product.ts
ā āāā ...
āāā index.ts # Schema exports
| Phase | Validate | Command |
|---|---|---|
| Schema defined | No TS errors | npx tsc --noEmit |
| Types inferred | z.infer works |
Check type in IDE |
| Validation works | safeParse tests |
pnpm test |
| Mistake | Issue | Fix |
|---|---|---|
| Manual type definitions | Type drift | Use z.infer<typeof Schema> |
Using .parse() directly |
Throws on invalid | Use .safeParse() for error handling |
Missing .optional() |
Runtime errors | Mark optional fields explicitly |
| Complex refinements | Hard to debug | Break into smaller schemas |
| Not using branded types | Type confusion | Use .brand() or transform for domain safety |
For up-to-date Zod patterns, use Context7 MCP:
mcp__context7__get-library-docs with context7CompatibleLibraryID: "/colinhacks/zod"
{baseDir}/src/modules/config/schema/schema.ts - Main schema definitions{baseDir}/docs/CODE_QUALITY.md#zod-first-development - Quality standardsadding-entity-types for full schema-to-service implementationanalyzing-test-coverage for test data builderswriting-graphql-operations for schema-to-GraphQL patternsFor a condensed quick reference, see .claude/rules/config-schema.md (automatically loaded when editing src/modules/config/**/*.ts files).