honojs-middleware/packages/class-validator
Jonathan Haines 4d67af162f
test(workspace): upgrade to vitest v3 (#1009)
* test(workspace): upgrade to vitest v3

Fixes #1007

* chore(standard-validator): add vitest type to `tsconfig.json`

* chore: update `yarn.lock`

* chore(zod-openapi): bump `typescript`

* chore(typia-validator): make it ESM

* ci(bun-transpiler): fix Bun to v1.1.32

---------

Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
2025-03-12 12:52:15 +09:00
..
src chore: use the latest eslint and `@hono/eslint-config` (#904) 2024-12-25 18:08:43 +09:00
test chore: use the latest eslint and `@hono/eslint-config` (#904) 2024-12-25 18:08:43 +09:00
CHANGELOG.md Version Packages (#816) 2024-11-08 12:26:21 +09:00
README.md feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +09:00
package.json test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00
tsconfig.json feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +09:00
tsconfig.vitest.json feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +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

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