honojs-middleware/packages/typia-validator
Musa Asukhanov 70bae1d573
fix: Move "default" entrypoint down in "typia-validator" (#1027)
* Move "default" entrypoint down in "typia-validator"

* Add changeset

* Fix changeset
2025-03-23 11:25:47 +09:00
..
src test(typia-validator): use `unplugin-typia` (#1000) 2025-03-05 14:13:40 +09:00
CHANGELOG.md Version Packages (#1025) 2025-03-19 18:07:38 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json fix: Move "default" entrypoint down in "typia-validator" (#1027) 2025-03-23 11:25:47 +09:00
tsconfig.cjs.json feat: Add Typia validator (#158) 2023-09-15 22:54:56 +09:00
tsconfig.esm.json feat: Add Typia validator (#158) 2023-09-15 22:54:56 +09:00
tsconfig.json chore: Migrate workspaces that use `jest` to `vitest` (#998) 2025-03-04 22:00:28 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

README.md

Typia validator middleware for Hono

codecov

The validator middleware using Typia for Hono applications.

Usage

You can use Basic Validation and HTTP Module Validation with Typia Validator.

Basic Validation

Use only the standard validator in typia.

import typia, { tags } from 'typia'
import { typiaValidator } from '@hono/typia-validator'

interface Author {
  name: string
  age: number & tags.Type<'uint32'> & tags.Minimum<20> & tags.ExclusiveMaximum<100>
}

const validate = typia.createValidate<Author>()

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

Hook:

app.post(
  '/post',
  typiaValidator('json', validate, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400)
    }
  })
  //...
)

HTTP Module Validation

Typia's HTTP module allows you to validate query and header parameters with automatic type parsing.

  • Supported Parsers: The HTTP module currently supports "query" and "header" validations.
  • Parsing Differences: The parsing mechanism differs slightly from Hono's native parsers. Ensure that your type definitions comply with Typia's HTTP module restrictions.
import { Hono } from 'hono'
import typia from 'typia'
import { typiaValidator } from '@hono/typia-validator/http'

interface Author {
  name: string
  age: number & tags.Type<'uint32'> & tags.Minimum<20> & tags.ExclusiveMaximum<100>
}

interface IQuery {
  limit?: number
  enforce: boolean
  values?: string[]
  atomic: string | null
  indexes: number[]
}
interface IHeaders {
  'x-category': 'x' | 'y' | 'z'
  'x-memo'?: string
  'x-name'?: string
  'x-values': number[]
  'x-flags': boolean[]
  'x-descriptions': string[]
}

const app = new Hono()

const validate = typia.createValidate<Author>()
const validateQuery = typia.http.createValidateQuery<IQuery>()
const validateHeaders = typia.http.createValidateHeaders<IHeaders>()

app.get(
  '/items',
  typiaValidator('json', validate),
  typiaValidator('query', validateQuery),
  typiaValidator('header', validateHeaders),
  (c) => {
    const query = c.req.valid('query')
    const headers = c.req.valid('header')
    return c.json({
      success: true,
      query,
      headers,
    })
  }
)

Author

Patryk Dwórznik https://github.com/dworznik

License

MIT