@brauni/req-validator
Express middleware to validate a request data.
Installation
$ npm install @brauni/req-validator
Quick Usage
Request example
{
"query": {
"id": "123456"
},
"body": {
"foo": "example",
"num": 10
},
}
Express server
const app = require('express')();
const { reqValidator } = require('@brauni/req-validator');
const options = {
id: { type: 'string', location: 'query.id' },
fooKey: { type: 'string', location: 'body.foo' },
numKey: { type: 'number', location: 'body.num' },
};
// optional parameter
const callback = (err, res) => {
if (err) {
console.error(err.stack);
res.status(400).send(err.message);
}
};
app.post('/', reqValidator(options, callback), (req, res) => {
console.log(req.dto);
res.sendStatus(200);
});
app.listen();
// console output
//
// dto {
// id: '123456',
// fooKey: 'example',
// numKey: 10
// }
reqValidator
Receive 2 parameters: options
and a callback
. The only required parameter is options
.
In options
you can define all rules that you want to validate by a JSON object.
Then an object called dto
will be attached to the request object that will contain all the parameters validated by this function.
options
parameter name
Name that the parameter receives in the data that is attached to the Request. If the location rule is not defined, the key must have the name of the original parameter.
rules
JSON object that contains the validation rules for this parameter. Here you can see all rules that you can define:
-
type
: Data type of the parameter, possible values:string
number
boolean
object
array
-
location
: [Optional] By defaultlocation
will be the same askey
. You can define alocation
to search the parameter in an object. Example:
// Request
{
"query": {
"id": "123456"
},
"body": {
"foo": 10
},
"params": {
"bars": []
},
}
// Rules
{
"body": { "type": "object" }, // location = body
"myId": { "type": "string", "location": "body.id" },
"foo": { "type": "number", "location": "body.foo" },
"bars": { "type": "array", "location": "body.bars" },
}
// Request dto
{
"body": {
"foo": 10
},
"myId": "123456",
"foo": 10,
"bars": []
}
-
default
: [Optional] By default, all parameters in validation options are required in request. If you define a default value, the parameter becomes optional. You can define default asnull
if you don't want to define a default value but if you want it to be an optional parameter. Default value must be the same type as thetype
property. -
item
: [Optional] Only works whentype
isarray
. You can pass new validation rules here. All items in an array parameter will be validated. Example:
// Rules
{
"foo": {
"type": "array",
"location": "body.foo",
"item": {
"type": "string"
},
},
}
// Request
{ "foo": [ "example" ] } // accepted
{ "foo": [ 5, 10 ] } // rejected
{ "foo": [ 5, "example", 10 ] } // rejected
-
dto
: [Optional] By defaultdto
is true. You can setdto
as false if you don't want attach this parameter to the Request dto object. -
regex
: [Optional] You can define a regular expression, the middleware will validate if the parameter match with the condition. It will return an error in case the condition fails.
callback [optional]
By default this middleware already has its own error handling but if you want to handle errors, you must pass a function as a second parameter. You will receive an error object and a response object.
const callback = (err, res) => {
if (err) {
console.error(err.stack);
res.status(400).send(err.message);
}
};
Error handling
You can also identify the errors that the middleware provides with the instanceof
operator and the error class. Here you can see a list of the error classes:
class | description |
---|---|
ReqValidatorError |
The rest of the classes inherit from this class, useful when you want to determine if the error comes from some validation of the middleware. |
PropertyRequiredError |
If a required property doesn't exist. |
RegexError |
If a regex validation fails. |
TypeError |
If the data type of a parameter doesn't match with the type property. |
Usage example:
const { ReqValidatorError } = require('@brauni/req-validator');
const callback = (err, res) => {
if (err instanceof ReqValidatorError) {
console.error(err.stack);
res.status(err.status).send(err.message);
} else {
console.error(err.stack);
res.status(500).send(err.message);
}
};