---
title: "Hono Validator with TypeBox Schemas"
sidebar: "Hono"
description: "Validate Hono requests with TypeBox through @hono/standard-validator: typed bodies and queries, coercion, defaults, and responses without writeOnly fields."
---

# Hono

Hono validates with any Standard Schema through its official middleware, `@hono/standard-validator`. A schema built with this package's `Type` goes straight in: there's no TypeBox-specific validator to install, and requests get this package's coercion, defaults and read/write projections.

<!-- tabs -->

**bun**

```sh
bun add hono @hono/standard-validator typebox-standard-schema
```

**npm**

```sh
npm install hono @hono/standard-validator typebox-standard-schema
```

<!-- /tabs -->

## A complete route

```ts
import { sValidator } from '@hono/standard-validator'
import { Hono } from 'hono'
import { parse, Type } from 'typebox-standard-schema'

const User = Type.Object({
  id: Type.Readonly(Type.Integer()),
  email: Type.String({ format: 'email' }),
  password: Type.Writeonly(Type.String({ minLength: 8 })),
})

const app = new Hono()

app.post('/users', sValidator('json', Type.Write(User)), async (c) => {
  const input = c.req.valid('json') // { email: string, password: string }
  const row = await db.insertUser(input) // { id, email, password }
  return c.json(parse(Type.Read(User), row), 201) // { id, email }
})

export default app
```

`db` stands for your data layer. Two projections of one schema do the work:

- `Type.Write(User)` is the request shape. `id` is not part of it: the handler's type has no `id`, and if a client sends one anyway, it is dropped before the handler runs.
- Hono doesn't check responses, so the route passes the row through `parse(Type.Read(User), row)`. The read projection drops `password`, and the response type is `{ id: number, email: string }`.

A cut field that is present is still [checked against its own schema](/typebox-standard-schema/validation.md#pipeline) before it goes. A stored bcrypt hash passes `minLength: 8`, but a `maxLength` or `pattern` written for the plaintext password would reject the row; leave the column out of the row in that case.

## Query parameters

Query values arrive as strings. [Coercion](/typebox-standard-schema/validation.md) turns them into the types the schema asks for, and defaults fill in what's missing:

```ts
const Search = Type.Object({
  q: Type.String({ minLength: 1 }),
  page: Type.Integer({ minimum: 1, default: 1 }),
})

app.get('/users', sValidator('query', Search), (c) => {
  const { q, page } = c.req.valid('query') // page: number, 1 when absent
  return c.json(db.searchUsers(q, page))
})
```

`?q=ann&page=2` reaches the handler as `{ q: 'ann', page: 2 }`; `?q=ann&page=abc` is rejected.

## Invalid input

A failed check answers `400` with `{ success: false, error, data }`, where `error` is the list of issues, each with a `path` to its field. Hono echoes the rejected input in `data`, the password included. To answer with the issues alone, pass a hook as the third argument:

```ts
const createUser = sValidator('json', Type.Write(User), (result, c) => {
  if (!result.success)
    return c.json({ issues: result.error }, 400)
})
```

## OpenAPI

For an OpenAPI document with `hono-openapi`, see [JSON Schema & OpenAPI](/typebox-standard-schema/json-schema.md#openapi-with-hono): the request body comes out without `id`, the response without `password`.
