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
)
app.post '/user', makeValidateBodyUser, // Will validate req.body against the User class
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:
- [REQUIRED] Class to validate (using class-validator decorators)
- [OPTIONAL] Custom error handler:
(err:{}, req, res, next) => void
License
See ./LICENSE