express-class-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Express Class Validator

This is a very simple middleware for using simplifying the use of class-validator in express routes.

It may not be worth installing this package

Please consider copy-pasting the source from ./lib/index.ts

Install

npm i express-class-validator

Usage

See ./sample/index.ts for a working example (npx ts-node sample/index.ts)

import { makeValidateBody } from 'express-class-validator'
 
class User {
    @IsEmail()
    public email!: string
 
    public hello(): string {
        return "World!"
    }
}
 
app.post(
    '/user',
    makeValidateBody(User), // Will validate req.body against the User class
    (req, res) => {
        console.info(`Got user:`, req.body)
        console.info(`user.hello():`, req.body.hello())
        res.send(req.body)
    }
)

Working Query

curl -X POST \
  http://localhost:1337/user \
  -H 'Content-Type: application/json' \
  -d '{"email":"bob@bob.com"}'

Failing Query

curl -X POST \
  http://localhost:1337/user \
  -H 'Content-Type: application/json' \
  -d '{"email":"NOTANEMAIL"}'

The makeValidateBody method takes two arguments:

  1. [REQUIRED] Class to validate (using class-validator decorators)
  2. [OPTIONAL] Custom error handler: (err:{}, req, res, next) => void

License

See ./LICENSE

Readme

Keywords

none

Package Sidebar

Install

npm i express-class-validator

Weekly Downloads

96

Version

1.0.3

License

ISC

Unpacked Size

12.6 kB

Total Files

8

Last publish

Collaborators

  • isnit