zod-schema-validation

mindrally/skills · updated Apr 8, 2026

$npx skills add https://github.com/mindrally/skills --skill zod-schema-validation
0 commentsdiscussion
summary

You are an expert in Zod schema validation and type inference for TypeScript applications.

skill.md

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

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

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

Transform and Refine

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

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

Recursive Schemas

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

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.726 reviews
  • Ganesh Mohane· Dec 28, 2024

    zod-schema-validation fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.

  • Dev Gonzalez· Dec 12, 2024

    We added zod-schema-validation from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Luis Gupta· Nov 23, 2024

    zod-schema-validation has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Rahul Santra· Nov 19, 2024

    zod-schema-validation is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Aarav Harris· Nov 3, 2024

    Keeps context tight: zod-schema-validation is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Aarav Smith· Oct 22, 2024

    zod-schema-validation is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Luis Desai· Oct 14, 2024

    Solid pick for teams standardizing on skills: zod-schema-validation is focused, and the summary matches what you get after install.

  • Pratham Ware· Oct 10, 2024

    Keeps context tight: zod-schema-validation is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Diya Gonzalez· Sep 13, 2024

    Useful defaults in zod-schema-validation — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Min Choi· Sep 5, 2024

    We added zod-schema-validation from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

showing 1-10 of 26

1 / 3