# @hono/zod-validator

> Validator middleware using Zod

Latest version **0.9.1** (published 2026-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hono/zod-validator
pnpm add @hono/zod-validator
yarn add @hono/zod-validator
bun add @hono/zod-validator
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.9.1 |
| Published | 2026-08-31 |
| First published | 2023-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 987 |
| Maintainers | yusukebe |

## Links

- npm: https://www.npmjs.com/package/@hono/zod-validator
- Repository: https://github.com/honojs/middleware
- npm.io page: https://npm.io/package/@hono/zod-validator

## Recent versions

- 0.9.1 (latest) — 2026-08-31
- 0.9.0 — 2026-07-15
- 0.8.0 — 2026-05-09
- 0.7.6 — 2025-12-18
- 0.7.5 — 2025-11-19
- 0.7.4 — 2025-10-10
- 0.7.3 — 2025-09-17
- 0.7.2 — 2025-07-19
- 0.7.1 — 2025-07-13
- 0.7.0 — 2025-05-30
- 0.6.0 — 2025-05-27
- 0.5.0 — 2025-04-27
- 0.4.3 — 2025-02-15
- 0.4.2 — 2024-12-13
- 0.4.1 — 2024-10-06
- … 22 more at https://npm.io/package/@hono/zod-validator/versions

## README

# Zod validator middleware for Hono

[![codecov](https://codecov.io/github/honojs/middleware/graph/badge.svg?flag=zod-validator)](https://codecov.io/github/honojs/middleware)

The validator middleware using [Zod](https://zod.dev) for [Hono](https://honojs.dev) applications. You can write a schema with Zod and validate the incoming values.

## Usage

```ts
import * as z from 'zod'
import { zValidator } from '@hono/zod-validator'

const schema = z.object({
  name: z.string(),
  age: z.number(),
})

app.post('/author', zValidator('json', schema), (c) => {
  const data = c.req.valid('json')
  return c.json({
    success: true,
    message: `${data.name} is ${data.age}`,
  })
})
```

Hook:

```ts
app.post(
  '/post',
  zValidator('json', schema, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400)
    }
  })
  //...
)
```

Throw Error:

throw a zod validate error instead of directly returning an error response.

```ts
// file: validator-wrapper.ts
import * as z from 'zod'
import type { ValidationTargets } from 'hono'
import { zValidator as zv } from '@hono/zod-validator'

export const zValidator = <T extends z.ZodSchema, Target extends keyof ValidationTargets>(
  target: Target,
  schema: T
) =>
  zv(target, schema, (result, c) => {
    if (!result.success) {
      throw new HTTPException(400, { cause: result.error })
    }
  })

// usage
import { zValidator } from './validator-wrapper'
app.post(
  '/post',
  zValidator('json', schema)
  //...
)
```

### Custom validation function

By default, this Validator validates values using `.safeParseAsync`.

```ts
await schema.safeParseAsync(value)
```

But, if you want to use the [`.passthrough`](https://zod.dev/?id=passthrough), you can specify your own function in `validationFunction`.

```ts
app.post(
  '/',
  zValidator('json', schema, undefined, {
    validationFunction: async (schema, value) => {
      return await schema.passthrough().safeParseAsync(value)
    },
  }),
  (c) => {
    // ...
  }
)
```

## Types

### `InferInput`

To infer the input type of a validated target, import `InferInput` from `hono/validator`:

```ts
import type { InferInput } from 'hono/validator'
```

## Author

Yusuke Wada <https://github.com/yusukebe>

## License

MIT

---
_Source: https://npm.io/package/@hono/zod-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
