express-validator
Advanced tools
Comparing version 3.1.3 to 3.2.0
@@ -52,45 +52,2 @@ var validator = require('validator'); | ||
/** | ||
* Initializes a chain of validators | ||
* | ||
* @class | ||
* @param {(string|string[])} param path to property to validate | ||
* @param {string} failMsg validation failure message | ||
* @param {Request} req request to attach validation errors | ||
* @param {string} location request property to find value (body, params, query, etc.) | ||
* @param {object} options options containing error formatter | ||
*/ | ||
function ValidatorChain(param, failMsg, req, location, options) { | ||
this.errorFormatter = options.errorFormatter; | ||
this.param = param; | ||
this.value = location ? _.get(req[location], param) : undefined; | ||
this.validationErrors = []; | ||
this.failMsg = failMsg; | ||
this.req = req; | ||
this.lastError = null; // used by withMessage to get the values of the last error | ||
return this; | ||
} | ||
/** | ||
* Initializes a sanitizer | ||
* | ||
* @class | ||
* @param {(string|string[])} param path to property to sanitize | ||
* @param {[type]} req request to sanitize | ||
* @param {[string]} locations request property to find value | ||
*/ | ||
function Sanitizer(param, req, locations) { | ||
this.values = locations.map(function(location) { | ||
return _.get(req[location], param); | ||
}); | ||
this.req = req; | ||
this.param = param; | ||
this.locations = locations; | ||
return this; | ||
} | ||
/** | ||
* Adds validation methods to request object via express middleware | ||
@@ -119,2 +76,135 @@ * | ||
/** | ||
* Initializes a chain of validators | ||
* | ||
* @class | ||
* @param {(string|string[])} param path to property to validate | ||
* @param {string} failMsg validation failure message | ||
* @param {Request} req request to attach validation errors | ||
* @param {string} location request property to find value (body, params, query, etc.) | ||
* @param {object} options options containing error formatter | ||
*/ | ||
function ValidatorChain(param, failMsg, req, location, options) { | ||
this.errorFormatter = options.errorFormatter; | ||
this.param = param; | ||
this.value = location ? _.get(req[location], param) : undefined; | ||
this.validationErrors = []; | ||
this.failMsg = failMsg; | ||
this.req = req; | ||
this.lastError = null; // used by withMessage to get the values of the last error | ||
return this; | ||
} | ||
/** | ||
* Initializes a sanitizer | ||
* | ||
* @class | ||
* @param {(string|string[])} param path to property to sanitize | ||
* @param {[type]} req request to sanitize | ||
* @param {[string]} locations request property to find value | ||
*/ | ||
function Sanitizer(param, req, locations) { | ||
this.values = locations.map(function(location) { | ||
return _.get(req[location], param); | ||
}); | ||
this.req = req; | ||
this.param = param; | ||
this.locations = locations; | ||
return this; | ||
} | ||
/** | ||
* validate an object using a schema, using following format: | ||
* | ||
* { | ||
* paramName: { | ||
* validatorName: true, | ||
* validator2Name: true | ||
* } | ||
* } | ||
* | ||
* Pass options or a custom error message: | ||
* | ||
* { | ||
* paramName: { | ||
* validatorName: { | ||
* options: ['', ''], | ||
* errorMessage: 'An Error Message' | ||
* } | ||
* } | ||
* } | ||
* | ||
* @method validateSchema | ||
* @param {Object} schema schema of validations | ||
* @param {Request} req request to attach validation errors | ||
* @param {string} loc request property to find value (body, params, query, etc.) | ||
* @param {Object} options options containing custom validators & errorFormatter | ||
* @return {object[]} array of errors | ||
*/ | ||
function validateSchema(schema, req, loc, options) { | ||
var locations = ['body', 'params', 'query', 'headers'], | ||
currentLoc = loc; | ||
for (var param in schema) { | ||
// check if schema has defined location | ||
if (schema[param].hasOwnProperty('in')) { | ||
if (locations.indexOf(schema[param].in) !== -1) { | ||
currentLoc = schema[param].in; | ||
} else { | ||
// skip params where defined location is not supported | ||
continue; | ||
} | ||
} else { | ||
currentLoc = loc === 'any' ? locate(req, param) : currentLoc; | ||
} | ||
var validator = new ValidatorChain(param, null, req, currentLoc, options); | ||
var paramErrorMessage = schema[param].errorMessage; | ||
var opts; | ||
if (schema[param].optional) { | ||
validator.optional.apply(validator, schema[param].optional.options); | ||
if (validator.skipValidating) { | ||
validator.failMsg = schema[param].optional.errorMessage || paramErrorMessage || 'Invalid param'; | ||
continue; // continue with the next param in schema | ||
} | ||
} | ||
for (var methodName in schema[param]) { | ||
if (methodName === 'in') { | ||
/* Skip method if this is location definition, do not validate it. | ||
* Restore also the original location that was changed only for this particular param. | ||
* Without it everything after param with in field would be validated against wrong location. | ||
*/ | ||
currentLoc = loc; | ||
continue; | ||
} | ||
if (methodName === 'errorMessage') { | ||
/* Also do not validate if methodName | ||
* represent parameter error message | ||
*/ | ||
continue; | ||
} | ||
validator.failMsg = schema[param][methodName].errorMessage || paramErrorMessage || 'Invalid param'; | ||
opts = schema[param][methodName].options; | ||
if (opts != null && !Array.isArray(opts)) { | ||
opts = [opts]; | ||
} | ||
validator[methodName].apply(validator, opts); | ||
} | ||
} | ||
} | ||
// _.set validators and sanitizers as prototype methods on corresponding chains | ||
@@ -362,93 +452,2 @@ _.forEach(validator, function(method, methodName) { | ||
/** | ||
* validate an object using a schema, using following format: | ||
* | ||
* { | ||
* paramName: { | ||
* validatorName: true, | ||
* validator2Name: true | ||
* } | ||
* } | ||
* | ||
* Pass options or a custom error message: | ||
* | ||
* { | ||
* paramName: { | ||
* validatorName: { | ||
* options: ['', ''], | ||
* errorMessage: 'An Error Message' | ||
* } | ||
* } | ||
* } | ||
* | ||
* @method validateSchema | ||
* @param {Object} schema schema of validations | ||
* @param {Request} req request to attach validation errors | ||
* @param {string} loc request property to find value (body, params, query, etc.) | ||
* @param {Object} options options containing custom validators & errorFormatter | ||
* @return {object[]} array of errors | ||
*/ | ||
function validateSchema(schema, req, loc, options) { | ||
var locations = ['body', 'params', 'query', 'headers'], | ||
currentLoc = loc; | ||
for (var param in schema) { | ||
// check if schema has defined location | ||
if (schema[param].hasOwnProperty('in')) { | ||
if (locations.indexOf(schema[param].in) !== -1) { | ||
currentLoc = schema[param].in; | ||
} else { | ||
// skip params where defined location is not supported | ||
continue; | ||
} | ||
} else { | ||
currentLoc = loc === 'any' ? locate(req, param) : currentLoc; | ||
} | ||
var validator = new ValidatorChain(param, null, req, currentLoc, options); | ||
var paramErrorMessage = schema[param].errorMessage; | ||
var opts; | ||
if (schema[param].optional) { | ||
validator.optional.apply(validator, schema[param].optional.options); | ||
if (validator.skipValidating) { | ||
validator.failMsg = schema[param].optional.errorMessage || paramErrorMessage || 'Invalid param'; | ||
continue; // continue with the next param in schema | ||
} | ||
} | ||
for (var methodName in schema[param]) { | ||
if (methodName === 'in') { | ||
/* Skip method if this is location definition, do not validate it. | ||
* Restore also the original location that was changed only for this particular param. | ||
* Without it everything after param with in field would be validated against wrong location. | ||
*/ | ||
currentLoc = loc; | ||
continue; | ||
} | ||
if (methodName === 'errorMessage') { | ||
/* Also do not validate if methodName | ||
* represent parameter error message | ||
*/ | ||
continue; | ||
} | ||
validator.failMsg = schema[param][methodName].errorMessage || paramErrorMessage || 'Invalid param'; | ||
opts = schema[param][methodName].options; | ||
if (opts != null && !Array.isArray(opts)) { | ||
opts = [opts]; | ||
} | ||
validator[methodName].apply(validator, opts); | ||
} | ||
} | ||
} | ||
/** | ||
* Validates and handles errors, return instance of itself to allow for chaining | ||
@@ -455,0 +454,0 @@ * |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -17,0 +17,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60432
11
1045