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.
bun
bun add hono @hono/standard-validator typebox-standard-schemanpm
npm install hono @hono/standard-validator typebox-standard-schemaA complete route
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 appdb stands for your data layer. Two projections of one schema do the work:
Type.Write(User)is the request shape.idis not part of it: the handler's type has noid, 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 dropspassword, and the response type is{ id: number, email: string }.
A cut field that is present is still checked against its own schema 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 turns them into the types the schema asks for, and defaults fill in what's missing:
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:
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: the request body comes out without id, the response without password.