express-params-validator
v1.0.0
Published
a middleware to validate express params on a route
Downloads
10
Readme
express-params-validator
a middleware to validate express params on a route
Installation
npm install express-params-validator
or
yarn add express-params-validator
Also make sure that you have Node.js 6 or newer in order to use it.
How to use
import { middleware } from 'express-params-validator';
const paramValidator = (req, res, next) => {
const valid = middleware(req, res, next);
valid
.isEnum('type', ['email', 'number'])
.isUUID('id')
.isNumber('phone')
.check();
};
export { paramValidator };
then you can use it in the express router like this
import express from 'express';
import message from '../controllers/message';
import { paramValidator } from '../middlewares/validator';
const { getMessage } = message;
const router = express.Router();
router.post('/messag/:id/:type/:phone', paramValidator, getMessage);
export default router;
and get errors as
{
"errors": [
"id must be a UUID",
"phone must be a number",
"type must be be one of these: email,number"
],
"message": "Invalid Parameter(s)"
}
To use custom error output
const paramValidator = (req, res, next, {
error: true,
message: 'there is an error',
data: {}
}) => {
const valid = middleware(req, res, next);
valid
.isEnum('type', ['email', 'number'])
.isUUID('id')
.isNumber('phone')
.check();
};
and get an error similar to
{
"error" : true,
"message": "there is an error",
"data": {"errors": [
"id must be a UUID",
"phone must be a number",
"type must be be one of these: email,number"
]
}
}
Changelog
Check the GitHub Releases page.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jojiAndela/express-params-validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
MIT License