
Zod Schema Validation
Validate API payloads, env config, and form data at boundaries with Zod schemas and inferred TypeScript types.
Overview
Zod Schema Validation is an agent skill for the Build phase that teaches Zod schemas, safe parsing, and TypeScript type inference at API and form boundaries.
Install
npx skills add https://github.com/mindrally/skills --skill zod-schema-validationWhat is this skill?
- Core patterns: object schemas, z.infer types, safeParse vs throw, transform and refine chains
- Validate at system boundaries—API routes, forms, and external JSON—not deep inside business logic only
- Compose schemas with extend, merge, pick, and omit; reuse base schemas for shared fields
- Form integration guidance alongside server-side safe parsing for typed error handling
- Guard clauses and early returns on validation failure for predictable agent-generated handlers
Adoption & trust: 650 installs on skills.sh; 133 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your agent emits TypeScript handlers with loose any types and no runtime checks, so bad client or webhook data crashes production logic.
Who is it for?
Indie TypeScript backends (Next.js routes, Hono, tRPC-style handlers) where you want schema-first types without a separate codegen step.
Skip if: Pure Python or Rust stacks, or projects standardized on Valibot, Yup, or JSON Schema-only pipelines.
When should I use this skill?
You are implementing or refactoring TypeScript boundaries that need Zod validation and z.infer types.
What do I get? / Deliverables
You get reusable Zod schemas, inferred types, and safeParse guard paths that reject invalid input before it reaches business rules.
- Zod schema modules with inferred types
- safeParse guard patterns for handlers
- Composed or extended schemas for shared DTOs
Recommended Skills
Journey fit
Build is where schemas wire HTTP handlers, server actions, and shared DTOs; validation is not a launch-only concern. Backend subphase is the canonical shelf because the skill emphasizes API and system-boundary parsing, with form patterns as a secondary surface.
How it compares
Schema-and-type playbook for agents—not an OpenAPI code generator or a hosted validation MCP.
Common Questions / FAQ
Who is zod-schema-validation for?
Solo builders and small teams using TypeScript who want their coding agent to produce consistent Zod patterns at API and form edges.
When should I use zod-schema-validation?
During Build when adding REST or server-action endpoints, webhook parsers, env/config loaders, or shared DTOs that must match frontend forms.
Is zod-schema-validation safe to install?
It is documentation-only procedural knowledge; review the Security Audits panel on this Prism page before trusting any third-party skill in prod repos.
SKILL.md
READMESKILL.md - Zod Schema Validation
# 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