Claude-skill-registry designing-zod-schemas
Designs Zod schemas following Zod-first development. Creates validation schemas, branded types, discriminated unions, and transforms. Infers TypeScript types from schemas. Triggers on: Zod schema, z.object, z.infer, validation, branded types, discriminated union, safeParse, refinement.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/designing-zod-schemas" ~/.claude/skills/majiayu000-claude-skill-registry-designing-zod-schemas && rm -rf "$T"
manifest:
skills/data/designing-zod-schemas/SKILL.mdsource content
Zod Schema Designer
Purpose
Guide the design and implementation of Zod schemas following the project's Zod-first development approach, where schemas are defined before implementing business logic.
When to Use
- Creating new data structures
- Adding validation to inputs
- Designing configuration schemas
- Implementing type-safe transformations
- Creating test data builders
Table of Contents
Core Principles
Zod-First Development
- Define Schema First: Schema is the source of truth
- Infer Types: Use
instead of manual type definitionsz.infer<> - Share Validation: Same schema for runtime validation and tests
- Compose Schemas: Build complex schemas from primitives
Decision Guide
| 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 | |
Quick Reference
Common Schema Shapes
import { z } from 'zod'; // String with constraints const NameSchema = z.string().min(1).max(100).trim(); // Slug pattern const SlugSchema = z.string().regex(/^[a-z0-9-]+$/); // Number with range const PriceSchema = z.number().min(0).multipleOf(0.01); // Object schema const EntitySchema = z.object({ name: z.string().min(1), slug: z.string().regex(/^[a-z0-9-]+$/), description: z.string().optional(), }); // Array with validation const TagsSchema = z.array(z.string()).min(1).max(10); // Type inference type Entity = z.infer<typeof EntitySchema>;
Safe Parsing
const result = schema.safeParse(data); if (!result.success) { // Handle errors console.error(result.error.issues); } else { // Use validated data const validData = result.data; }
Schema Patterns
For detailed patterns and examples, see:
- Primitive Patterns: Strings, numbers, enums, branded types
- Object Patterns: Objects, discriminated unions, arrays, transforms, refinements
- Testing Patterns: Test data builders, schema validation tests
Project Schema Location
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
Validation Checkpoints
| Phase | Validate | Command |
|---|---|---|
| Schema defined | No TS errors | |
| Types inferred | works | Check type in IDE |
| Validation works | tests | |
Common Mistakes
| Mistake | Issue | Fix |
|---|---|---|
| Manual type definitions | Type drift | Use |
Using directly | Throws on invalid | Use for error handling |
Missing | Runtime errors | Mark optional fields explicitly |
| Complex refinements | Hard to debug | Break into smaller schemas |
| Not using branded types | Type confusion | Use or transform for domain safety |
External Documentation
For up-to-date Zod patterns, use Context7 MCP:
mcp__context7__get-library-docs with context7CompatibleLibraryID: "/colinhacks/zod"
References
- Main schema definitions{baseDir}/src/modules/config/schema/schema.ts
- Quality standards{baseDir}/docs/CODE_QUALITY.md#zod-first-development- Zod documentation: https://zod.dev
Related Skills
- Complete entity workflow: See
for full schema-to-service implementationadding-entity-types - Testing schemas: See
for test data buildersanalyzing-test-coverage - GraphQL type mapping: See
for schema-to-GraphQL patternswriting-graphql-operations
Quick Reference Rule
For a condensed quick reference, see
.claude/rules/config-schema.md (automatically loaded when editing src/modules/config/**/*.ts files).