Course contentsShow
← TypeScript
Lesson 1535 of 1,79635. TypeScript with Framework Cross-ReferencesFree lesson

Zod Validators with Hono Middleware

Integrating @hono/zod-validator to type-check and validate request bodies, params, and queries.

Zod Validators with Hono Middleware

You'll learn how to integrate @hono/zod-validator to automatically validate and type-check incoming HTTP requests in Hono, ensuring runtime safety matches compile-time types.


What Problem Does This Solve?

In the previous lesson, you learned about path parameters in Hono. But what happens when users send malformed JSON bodies, invalid query strings, or incorrect route parameters? Without validation, your handlers receive unknown data that could crash your application or create security vulnerabilities.

@hono/zod-validator bridges the gap between Zod schemas and Hono middleware, giving you:

  • Automatic validation before your handler runs
  • Type inference from Zod schemas directly into your request context
  • Consistent error responses when validation fails
  • Single source of truth for both runtime checks and TypeScript types

How It Works

The @hono/zod-validator package provides a zValidator middleware function. You pass it:

  1. The location of data to validate ('json', 'query', 'param', 'header', etc.)
  2. A Zod schema defining the expected shape

If validation succeeds, Hono's context is automatically typed with the validated data. If it fails, Hono returns a 400 error with validation details—your handler never runs.


The Pattern

import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { z } from 'zod'

const app = new Hono()

// Define your schema
const userSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  age: z.number().min(18).optional()
})

// Use zValidator as middleware
app.post('/users', zValidator('json', userSchema), (c) => {
  // c.req.valid('json') is fully typed from userSchema!
  const user = c.req.valid('json')
  
  return c.json({ message: 'User created', user })
})

Notice you don't write manual validation logic—the middleware handles it. The c.req.valid('json') method gives you the validated data with full TypeScript inference from your Zod schema.


Validating Different Parts of Requests

You can validate query parameters, route params, headers, and bodies:

const querySchema = z.object({
  page: z.string().transform(Number),
  limit: z.string().transform(Number).optional()
})

app.get('/items', zValidator('query', querySchema), (c) => {
  const { page, limit } = c.req.valid('query')
  // Both are typed correctly (page: number, limit?: number)
})

Each validator location corresponds to a part of the HTTP request, and Hono merges all validated data into the context type.


Why This Matters

Without zValidator, you'd manually parse, check types, and handle errors for every endpoint. With it, your validation schemas become executable documentation that TypeScript understands. Invalid requests never reach your business logic, and your handlers work with fully validated, typed data—no assertions needed.


Key Takeaway: @hono/zod-validator turns Zod schemas into Hono middleware that validates requests and infers TypeScript types, giving you type-safe, runtime-validated request handling with minimal boilerplate.