honojs-middleware/packages/class-validator
Pr0m3th3us a5c20b3428
feat: class validator middleware for Hono (#788)
* setup middleware package

* add implementation middleware

* add documentation + updae tsconfig for decorator handling

* add tests

* fix format class-validator middleware

* Add Readme

* Update changelog & changeset

* update changelog 2

* update changelog 2

* fix working directory ci

* rm jest dependencies

change to tsup for build

fix ci name

* revert changes not related to class-validator

* remove the changeset since Changesets will add a changeset automatically

* package description
2024-11-08 12:08:18 +09:00
..
src feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +09:00
test feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +09:00
README.md feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +09:00
package.json feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +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 feat: class validator middleware for Hono (#788) 2024-11-08 12:08:18 +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