klh-zod-validation

Implements Zod schema validation patterns for TypeScript API inputs and data.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Validating external data at API boundaries is error-prone when done manually, leading to runtime crashes and inconsistent error handling. This Skill provides ready-to-use Zod schema patterns so TypeScript developers can validate inputs, infer types, and handle errors consistently. ## Core Features & Use Cases - Schema Building Blocks: Patterns for primitives, objects, arrays, unions, enums, and discriminated unions with type inference via z.infer. - API & Environment Validation: Request body schemas, query parameter coercion, and environment variable validation with safeParse and flattened error output. - Framework Integration: Fastify route validation using zod-to-json-schema, plus custom refinements, async validations, and cross-field checks. - Use Case: When building a Fastify endpoint that accepts user registration data, apply the provided CreateUserSchema pattern to validate email, password strength, and name, returning structured 400 errors on failure. ## Quick Start Ask the agent to add Zod validation to your API route or environment config using the patterns from this skill.

Frequently Asked Questions about klh-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 for the expected body shape, then call safeParse on the incoming request body. On failure, return a 400 response with error.flatten() to send structured field-level error messages to the client.

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

Create a Zod schema describing each environment variable with types and defaults, then safeParse process.env at startup. If validation fails, log the flattened field errors and exit the process before the app runs with invalid config.

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 validation for body and response.

How do I validate query parameters that arrive as strings?

Use z.coerce.number() or z.coerce.date() to automatically convert string query parameters into typed values during parsing. You can chain defaults and range checks, such as z.coerce.number().int().min(1).max(100).default(20).

Can Zod run async validations like checking unique emails?

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