Skip to content

Error handling in Validator

By using a validator, you can handle invalid input more easily.


You can utilize the callback result for implementing custom error handling.

Although this snippet employs Zod Validator, you can apply a similar approach with any supported validator library.

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

const app = new Hono()

const userSchema = z.object({
  name: z.string(),
  age: z.number(),
  zValidator('json', userSchema, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400)
  async (c) => {
    const user = c.req.valid('json')
    console.log( // string
    console.log(user.age) // number


Released under the MIT License.