Skip to main content

Overview

Hono provides a powerful and flexible validation system that allows you to validate incoming request data from various sources including JSON bodies, form data, query parameters, headers, and cookies.

Basic Validation

The validator middleware extracts data from different parts of the request and validates it using a custom validation function.

Validation Targets

You can validate data from these sources:
  • json - JSON request body
  • form - Form data (multipart or URL-encoded)
  • query - Query parameters
  • param - Path parameters
  • header - Request headers
  • cookie - Cookies

Basic Example

Query Parameter Validation

Validate URL query parameters:

Form Data Validation

Validate form data submissions:

Multiple Validators

Chain multiple validators to validate different parts of the request:

Using Zod

For more complex validation, use Zod with Hono’s validator:

Custom Error Messages

Return custom error responses:

Validation Utilities

The validator automatically handles:
  • Content-Type detection for JSON (application/json)
  • Form data parsing (both multipart/form-data and application/x-www-form-urlencoded)
  • Array handling in form data (fields ending with [])
  • Type safety with TypeScript

Best Practices

Never trust data from clients. Always validate and sanitize input before processing.
Leverage TypeScript’s type system along with validation to catch errors early.
Provide clear, actionable error messages to help API consumers fix issues.
For complex validation logic, use libraries like Zod, Valibot, or TypeBox with Hono validators.