Loading...
Loading...
Designs Zod schemas following Zod-first development. Use when creating validation schemas, branded types, discriminated unions, transforms, refinements, or inferring TypeScript types with z.infer.
npx skill4agent add saleor/configurator designing-zod-schemasz.infer<>| Need | Pattern | Example |
|---|---|---|
| Basic validation | Primitives | |
| Domain safety | Branded types | |
| Multiple types | Discriminated union | |
| Cross-field rules | Refinement | |
| Data normalization | Transform | |
| Partial updates | Partial schema | |
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>;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 | |
| Types inferred | | Check type in IDE |
| Validation works | | |
| Mistake | Issue | Fix |
|---|---|---|
| Manual type definitions | Type drift | Use |
Using | Throws on invalid | Use |
Missing | Runtime errors | Mark optional fields explicitly |
| Complex refinements | Hard to debug | Break into smaller schemas |
| Not using branded types | Type confusion | Use |
mcp__context7__get-library-docs with context7CompatibleLibraryID: "/colinhacks/zod"{baseDir}/src/modules/config/schema/schema.ts{baseDir}/docs/CODE_QUALITY.md#zod-first-developmentadding-entity-typesanalyzing-test-coveragewriting-graphql-operations.claude/rules/config-schema.mdsrc/modules/config/**/*.ts