Plugin for validating field values of json request in expressjs

Dependency Status Table of Contents generated with DocToc


$ npm install expressjs-field-validator


How To Use

 const { validator } = require('expressjs-field-validator');
validator([{param : 'id', location : 'params', isRequired : true}], { mode : 'reject', errorCode : '422' }),
(req, res, next) => {


validator arguments

Argument Type Description
validator Object[] Array of validation object
Response Object This Object determines the proceeding to the next step

validator Object

Property Type Description
param String Field name
location String Location of the field (body/params/query)
children Object[] Array of Child validator objects, only applicable if the field is Array or Object
isArray Boolean The value is Array or not (default false)
isObject Boolean The value is Object or not (default false)
isRequired Boolean The value is mandatory or not (default false)
isNumber Boolean The value is Number or not (default false)
range Object Object {min : 1, max : 10} describes minimum and maximum value of a Number field
isEmail Boolean The value is Email or not (default false)
isBoolean Boolean The value is Boolean or not (default false)
isDate Boolean The value is Date or not (default false)
mobileNumber Object Object {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} ,describes characteristics of mobile number, length is the length range of mobile number excluding country code
format String Date format
regEx RegEx Regular Expression to match with field
length Object Object {min : 1, max : 10} describes minimum and maximum length
includes Object[] Value must be one of the element in the array
excludes Object[] Value must not be one of the element in the array
message String Error message thrown in case of test fails default : Invalid Field Error

Nested Objets

In case of Object or Array, isArray or isObject must be true if json structure is

  "page" : {
    "sorted" : "True"
  "sort" : [{
    "value" : [{
	"date" : "2019-01-01",
	"length" : {"min" : "1", "max" : "100"}

the validator object

  {param : 'page', location : 'body', isObject : true, children : [
    {param : 'sorted', location : '', isRequired : true, isBoolean : true, message='Mandatory field page missing'},
  {param : 'sort', location : 'body', isArray : true, children : [
    {param : 'value', location : 'body.sort', isArray : true, children : [
      {param : 'date', location : 'body.sort.value', isRequired : true, isDate : true},
      {param : 'length', location : 'body.sort.value', isObject : true, children : [
        {param : 'min', location : 'body.sort.value.length', isNumber : true},
        {param : 'max', location : 'body.sort.value.length', isNumber : true}

Response object

Property Type Description
mode String can be reject or forward, Mandatory field
errorCode String Error code send in response. default 422 Error
debug Boolean set true to respond back more details on error


Value can be can be reject or forward.


Response is sent back with http status code provided in errorCode property

    "error": [
            "location": "body.sort",
            "param": "sort",
            "message": "Invalid Field Error"

Error is set to and error code to request.locals.statusCode. Forward the request to next middleware.


If debug is set to true, error response will be

    "error": [
            "location": "body.sort",
            "param": "sort",
            "message": "Invalid Field Error :: somevalueforsort Must Be A Boolean"

It will give more idea about the error.


 const { checkService } = require('expressjs-field-validator');

It helps to skip the main service function, if you have used forward mode.


Pass your service function to checkService, which must be skipped.

checkService((req, resp, next) => {