honojs-middleware/packages/class-validator
Jonathan Haines b18f24379b
chore(dev-deps): upgrade to hono v4 (#1092)
* chore(dev-deps): upgrade to hono v4

* chore(zod-openapi): build workspace dependencies

* chore(trpc-server): ignore null body type
2025-03-31 18:20:57 +09:00
..
src test: move tests to src directory (#1075) 2025-03-28 18:50:19 +09:00
CHANGELOG.md Version Packages (#816) 2024-11-08 12:26:21 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json chore(dev-deps): upgrade to hono v4 (#1092) 2025-03-31 18:20:57 +09:00
tsconfig.json build(class-validator): lint published package (#1041) 2025-03-27 10:38:31 +09:00
tsconfig.vitest.json build(class-validator): lint published package (#1041) 2025-03-27 10:38:31 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

README.md

Class-validator middleware for Hono

codecov

The validator middleware using class-validator for Hono applications.

Usage

import { classValidator } from '@hono/class-validator'
import { IsInt, IsString } from 'class-validator'

class CreateUserDto {
  @IsString()
  name!: string

  @IsInt()
  age!: number
}

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

With hook:

import { classValidator } from '@hono/class-validator'
import { IsInt, IsString } from 'class-validator'

class CreateUserDto {
  @IsString()
  name!: string

  @IsInt()
  age!: number
}

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

Author

Pr0m3ht3us - https://github.com/pr0m3th3usex

License

MIT