Skip to content

vikcch/layer-one-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

layer-one-validator

Some expressjs middleware callback functions to validate data from requests, checks the amount of properties, their types and the business logic.

Installation and Usage

Server-side usage

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 the biz:function, testing can be performed on the entire array or on each individual element.

Syntax

layerOneValidator.body.bind(object)

layerOneValidator.body.bind(objectsArray)

Object

{ 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.

Body, Query or Params

The layer-one-validator is an object with 3 properties, 'body','params' and 'query', representing each express request.

Responses

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 as layer-one-validator.

If successful, will go to the next middleware.

HTTP response status codes

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 only

// 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 & Helpers & Controller

// 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

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

License

Licensed under the MIT