express-dee-validator
v3.0.2
Published
Object fields validator for express framework
Downloads
317
Maintainers
Readme
Express Dee Validator
Dee-validator port for Express framework.
Table of contents
Migration to v2-v3
The v1 doesn't support async validators meaning the API is synchronous.
For migration to v2/v3, await getErrors and hasErrors methods.
Usage
The middleware creates validator which contains three dee-validators for req.body, req.query and req.params objects.
You can use each validator separately.
The example of code:
const express = require('express');
const validator = require('express-dee-validator');
const app = express();
const customValidators = { // custom validators
isTestString: {
execute: value => value === 'test'
}
}
app.use(validator(customValidators));
app.use(async (req, res, next) => {
const validator = req.validator;
const { bodyValidator, paramsValidator, queryValidator } = validator;
console.log(validator.request); // you can get request object from the req.validator
bodyValidator.property('name').isNotEmpty().isTestString();
paramsValidator.property('id').isNotEmpty();
queryValidator.property('test').optional().isUpperCaseString();
if (await validator.hasErrors()) { // return true in case if no errors in body, params and query validators
next({
errors: await validator.getErrors() // here you can get errors from all of the validators
});
} else {
next();
}
})You can find more details about creation of custom validators and a validator usage here.
Example of errors format:
{
'name': {
param: 'name',
message: 'name should be a string',
value: 0
},
'id': {
param: 'id',
message: 'id should be an integer',
value: 'test'
}
}Author
Ilya Markevich
