Some expressjs middleware callback functions to validate data from requests, checks the amount of properties, their types and the business logic.
Install the library with:
npm install layer-one-validator
The validator should be called using the bind()
method. The first and only argument could be an object
or an array
.
NOTE:: If the value being tested is an array, the
type:function
must be examined for each element within that array. In the case of thebiz:function
, testing can be performed on the entire array or on each individual element.
layerOneValidator.body.bind(object)
layerOneValidator.body.bind(objectsArray)
{ prop:string [, type:function][, biz:function] }
{ prop:string [, type:function][, biz:function] }[]
-
prop <string> - The name of the property named on the request.
-
type <function> - Optional, a function to test the type of the property. Should return a
boolean
. -
biz <function> - Optional, a function to test the business part of the property. Should return a
boolean
.
The layer-one-validator is an object with 3 properties, 'body'
,'params'
and 'query'
, representing each express request.
When fails, the response will be an json object, with some properties:
-
{ success: false }
- Will throw an error on the server with additional information. -
{ /* ... */, message }
- Indicates the stage at which the failures occurred. -
{ /* ... */, fail }
- Displays the name of the failing property. -
{ /* ... */, layer }
- Displays'body'
,'params'
or'query'
. -
{ /* ... */, source }
- Displays the source aslayer-one-validator
.
If successful, will go to the next middleware.
If the bound object or objectsArray is incorrect, or if the type
property function checks the entire array instead of individual items:
500 Internal Server Error
When there are issues with prop, type or inconsistencies in property quantities:
400 Bad Request
When fails on biz:
422 Unprocessable Content
// route
const layerOneValidator = require('layer-one-validator');
router.post('/user',
layerOneValidator.body.bind([
{ prop: 'weight', type: v => Number.isInteger(v), biz: v => v > 0 },
{ prop: 'username', biz: v => /^[a-z]{4,8}$/.test(v) }
]),
(request, response, next) => {
/* ... */
}
);
module.exports = router;
// route
const userController = require(/* ... */);
router.post('/user',
userController.user.validation.layerOne,
userController.user.execute
);
// helpers - fns.js
module.exports = {
isNumber: value => typeof value === 'number',
isString: value => typeof value === 'string'
};
// helpers - biz.js
module.exports = {
isWeight: value => Number.isInteger(value) && value > 0,
isUsername: value => /^[a-z]{4,8}$/.test(value)
};
// controller
const layerOneValidator = require('layer-one-validator');
const fns = require('./path_to_helpers/fns');
const biz = require('./path_to_helpers/biz');
module.exports = {
// * POST
user: {
validation: {
layerOne: layerOneValidator.body.bind([
{ prop: 'weight', type: fns.isNumber, biz: biz.isWeight },
{ prop: 'username', type: fns.isString, biz: biz.isUsername }
])
},
execute(request, response, next) {
/* ... */
}
}
};
Tests are using mocha, to run the tests use:
$ npm test
Conducts tests without displaying layer-one-validator helper errors in the console.
$ npm run test-no-print-error
Licensed under the MIT