---
title: "Request Data Validation"
description: "Hono provides a built-in validator middleware that lets you inspect and validate incoming request parameters, query values, headers, payloads, and cookies with end-to-end type safety. Validation wo..."
last_updated: "2026-11-24T23:01:51.035909+00:00"
canonical_url: "https://www.doc0.dev/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/user-guide/request-data-validation"
---

## Overview

Hono provides a built-in `validator` middleware that lets you inspect and validate incoming request parameters, query values, headers, payloads, and cookies with end-to-end type safety. Validation works seamlessly with schema-driven validation libraries (such as Zod) so that your route handlers receive fully typed, trusted data.

```mermaid
flowchart TD
    A[Incoming Request] --> B[Validator Middleware]
    B -->|Validation Fails| C[Throw 400 Bad Request / Custom Response]
    B -->|Validation Succeeds| D[Store Validated Data on Request]
    D -->|Route Handler| E[Access via c.req.valid target]
```

## Supported Validation Targets

The `validator` middleware supports six distinct parts of an incoming request. 

| Target | Description | Content Type / Source |
| :--- | :--- | :--- |
| `json` | JSON request payload | `application/json` |
| `form` | Form data / multipart payloads | `multipart/form-data`, `application/x-www-form-urlencoded` |
| `query` | Query string parameters | URL search parameters |
| `param` | Route path parameters | URL path segments |
| `header` | HTTP headers | Request headers |
| `cookie` | Request cookies | Cookie header |

> [!NOTE]
> `GET` and `HEAD` requests cannot carry a body content. Attempting to use body-based validation targets like `json` or `form` on `GET` or `HEAD` routes is restricted at compile time.

## Step-by-Step Instructions

### 1. Import the validator helper
Import `validator` from the core validator package:

```typescript
import { validator } from 'hono/validator'
```

### 2. Apply the validator middleware to a route
Pass the validation target and a validation callback function to your route definition. The callback receives the raw value for that target and the context object `c`.

```typescript
app.post(
  '/posts',
  validator('json', (value, c) => {
    // Perform your validation checks here
    if (!value || typeof value.title !== 'string') {
      return c.text('Invalid title', 400)
    }
    return value
  }),
  (c) => {
    const body = c.req.valid('json')
    return c.json({ message: `Created post: ${body.title}` })
  }
)
```

### 3. Retrieve validated data in your handler
Inside your route handler, call `c.req.valid(target)` to access the sanitized, typed data returned by your validation function.

```typescript
app.get(
  '/search',
  validator('query', (value, c) => {
    return {
      q: value.q || '',
      limit: value.limit ? Number(value.limit) : 10,
    }
  }),
  (c) => {
    const { q, limit } = c.req.valid('query')
    return c.json({ query: q, limit })
  }
)
```

## Key Concepts

* **Validation Function Signature:** A validation function has the signature `(value, c) => OutputType | TypedResponse | Promise<OutputType> | Promise<TypedResponse>`. If it returns a `Response` (or a `TypedResponse`), Hono aborts the request chain immediately and returns that response to the client.
* **Automatic Parsing:** For `json` targets, Hono automatically parses the incoming request body as JSON and catches malformed payloads, throwing a `400` status error if parsing fails. For `form` targets, it parses URL-encoded or multipart form data into a structured record.

> [!TIP]
> You can integrate third-party schema validation libraries (such as Zod) inside the `validator` callback by passing your schema's parse or safeParse result directly.

> [!WARNING]
> Do not attempt to use `json` or `form` validation targets on `GET` or `HEAD` requests. HTTP specifications disallow bodies on these methods, and Hono's type system will prevent it.

## Related

- [Context API Methods](https://www.doc0.dev/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/user-guide/context-api-methods)
- [Type-Safe RPC Client](https://www.doc0.dev/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/user-guide/type-safe-rpc-client)


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/llms.txt) for all pages in this wiki.
