Skip to content

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.

  • 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
Terminal window
deno add jsr:@yelix/zod-validator npm:zod
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!

zValidatorYelix supports validating data from different sources:

  • json - Request body as JSON
  • form - Form data (multipart/form-data or application/x-www-form-urlencoded)
  • query - URL query parameters
  • param - URL path parameters
  • header - HTTP headers
  • cookie - HTTP cookies