zod-validation

Implements Zod schema validation patterns for API inputs, environment variables, and data transformation.

1|Updated Apr 16, 2026
One-click install
npx skills add https://github.com/klh/skills --skill zod-validation-klh
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: zod-validation
Source: https://github.com/klh/skills/tree/main/.well-known/agent-skills/zod-validation
Command: npx skills add https://github.com/klh/skills --skill zod-validation-klh

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires zod, zod-to-json-schema.

What problem does it solve? Validating external data at application boundaries is error-prone when done manually, leading to runtime crashes and inconsistent error messages. This Skill provides ready-to-use Zod schema patterns so TypeScript developers can validate API inputs, environment variables, and data transformations with full type inference. ## Core Features & Use Cases - Schema Building Blocks: Covers primitives, objects, arrays, unions, discriminated unions, enums, and schema composition with pick, omit, partial, and extend. - API & Environment Validation: Includes request body schemas, query parameter coercion, Fastify route integration with zod-to-json-schema, and fail-fast environment variable validation. - Custom & Async Validation: Demonstrates refine-based custom rules, cross-field checks like password confirmation, and async validations such as unique email checks. - Use Case: When building a Fastify endpoint that accepts user registration data, apply the provided patterns to validate the request body, return flattened field errors on failure, and infer the TypeScript input type directly from the schema. ## Quick Start Ask the agent to create a Zod schema that validates a user registration request body with email, password rules, and matching password confirmation.

Frequently Asked Questions about zod-validation

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate API request bodies with Zod?▼

Define a Zod object schema describing the expected fields, then call safeParse on the incoming request body. On failure, use error.flatten() to return structured field errors with a 400 status; on success, use the parsed, typed data directly.

How to validate environment variables in Node.js with Zod?▼

Create a Zod schema for process.env using z.coerce.number() for ports and z.enum() for fixed values like NODE_ENV. Run safeParse at startup and exit the process with logged field errors if validation fails, ensuring fail-fast configuration.

Does Zod work with Fastify route validation?▼

Yes, Zod integrates with Fastify in two ways: manually calling safeParse inside the handler, or converting schemas with zod-to-json-schema and passing them to Fastify's built-in schema option so Fastify validates requests and responses automatically.

Can Zod handle async validation like checking unique emails?▼

Yes, Zod supports async validation through the refine method with an async callback, such as querying a database to check whether an email already exists. Note that async refinements require using parseAsync or safeParseAsync instead of the synchronous parse methods.

How do I validate that two password fields match in Zod?▼

Use the refine method on the object schema to compare the password and confirmPassword fields. Return false when they differ and set the path option to confirmPassword so the error message attaches to the correct field.

What are the limitations of Zod schema validation?▼

Zod is synchronous by default, so async refinements require separate async parse methods. Schemas are immutable, meaning every chained method returns a new schema, and JSON Schema conversion for OpenAPI requires the separate zod-to-json-schema package.