Zod Schema Validation

by mindrally97184105b5daNo license269 starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated 5 weeks ago

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

Instructions onlySoftware Development
AI-generated overview

Guidance on Zod schema validation and TypeScript type inference for application code.

What it does
This skill provides best-practice guidance for using Zod to validate data and infer TypeScript types. It covers schema design, safe parsing, transforms and refinements, form and API validation, error handling, and advanced patterns such as discriminated unions and recursive schemas. It produces advisory instructions and code examples rather than executable artifacts.
When to use it
Use it when writing or reviewing TypeScript code that validates data at system boundaries, such as API routes, forms, or external data. It is also relevant when deriving TypeScript types from Zod schemas or structuring validation errors.
Requirements
No scripts or tooling are bundled; it is instructions only. Applying the guidance assumes a TypeScript project using the Zod library, with optional packages such as react-hook-form and @hookform/resolvers/zod for form integration.

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

typescript
import { z } from 'zod'
const UserSchema = z.object({  id: z.string().uuid(),  email: z.string().email(),  name: z.string().min(1).max(100),  age: z.number().int().positive().optional(),  role: z.enum(['admin', 'user', 'guest']),  createdAt: z.date(),})
type User = z.infer<typeof UserSchema>

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

typescript
const result = UserSchema.safeParse(data)if (!result.success) {  console.error(result.error.format())  return}// result.data is typed as User

Transform and Refine

typescript
const schema = z.string()  .transform((val) => val.trim().toLowerCase())  .refine((val) => val.length > 0, 'Cannot be empty')

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

typescript
const ResultSchema = z.discriminatedUnion('status', [  z.object({ status: z.literal('success'), data: UserSchema }),  z.object({ status: z.literal('error'), message: z.string() }),])

Recursive Schemas

typescript
const CategorySchema: z.ZodType<Category> = z.lazy(() =>  z.object({    name: z.string(),    children: z.array(CategorySchema),  }))

Performance

  • Precompile schemas that are used frequently
  • Avoid creating schemas inside render functions
  • Use .passthrough() or .strict() intentionally
  • Consider partial validation for large objects

Source and attribution

Source:mindrally/skillsinzod-schema-validationat commit9718410

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal