Zod Validator Overview
The @yelix/zod-validator package provides request validation middleware that integrates seamlessly with Yelix and automatically generates OpenAPI schemas from your Zod validation rules.
Features
Section titled “Features”- Type-Safe Validation: Validate requests using Zod schemas with full TypeScript support
- Multiple Data Sources: Validate data from JSON body, query parameters, path parameters, headers, cookies, and form data
- Automatic OpenAPI Generation: Automatically converts Zod schemas to OpenAPI schemas for documentation
- Seamless Integration: Works perfectly with Yelix’s OpenAPI documentation system
- Error Handling: Built-in error handling for validation failures
Installation
Section titled “Installation”deno add jsr:@yelix/zod-validator npm:zodQuick Example
Section titled “Quick Example”import { YelixHono } from '@yelix/hono';import { zValidatorYelix, type ZInfer } from '@yelix/zod-validator';import { z } from 'zod';
const app = new YelixHono();
const userSchema = z.object({ name: z.string().min(1), email: z.email(), age: z.number().int().positive(),});
app.post( '/users', zValidatorYelix('json', userSchema), (c) => { // ZInfer provides full type safety const data: ZInfer<typeof userSchema> = c.req.valid('json' as never); return c.json({ message: 'User created', data }); });Note: The as never is needed due to TypeScript limitations with middleware type inference, but ZInfer ensures your data is fully type-safe!
Validation Sources
Section titled “Validation Sources”zValidatorYelix supports validating data from different sources:
json- Request body as JSONform- Form data (multipart/form-data or application/x-www-form-urlencoded)query- URL query parametersparam- URL path parametersheader- HTTP headerscookie- HTTP cookies
Next Steps
Section titled “Next Steps”- Learn about Validating JSON Bodies
- See Query and Path Parameters
- Explore Advanced Validation