zod-schema-validation

Best practices for Zod schema validation and type inference in TypeScript applications.

By mindrally · 1,065 installs

npx skills add mindrally/skills --skill zod-schema-validation

Source repository · Upstream listing

Zod Schema Validation You are an expert in Zod schema validation and type inference for TypeScript applications. Core Principles Utilize Zod for schema validation and type inference Validate data at system boundaries (API, forms, external data) Leverage TypeScript type inference from Zod schemas Implement early returns and guard clauses for validation errors Schema Design Basic Schema Best Practices Define schemas close to where they're used Use .infer to derive TypeScript types Compose schemas using .extend() , .merge() , .pick() , .omit() Create reusable base schemas for common patterns Validation Patterns Safe Parsing Transform and Refine Form Integration Use Zod with react hook form via @hookform/resolvers/zod Define form schemas that match your form structure Handle validation errors in UI appropriately Use .partial() for optional update forms API Validation Validate request bodies in API routes Validate query parameters and path params Return structured error responses Use discriminated unions for different response types Error Handling Implement custom error messages for better UX Use .format() for structured error output Create custom error maps for i18n support Handle nested object errors appropriately Advanced Patterns Discriminated Unions Recursive Schemas Performance Precompile schemas that are used frequently Avoid creating schemas inside render functions Use .passthrough() or .strict() intentionally Consider partial validation for large objects