Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-validator

Package Overview
Dependencies
Maintainers
2
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-validator - npm Package Compare versions

Comparing version 2.15.1 to 2.16.0

test/checkBodySchemaTest.js

0

CHANGELOG.md

@@ -0,0 +0,0 @@ ## Change Log

@@ -9,3 +9,3 @@ var validator = require('validator');

/**
* Initalizes a chain of validators
* Initializes a chain of validators
*

@@ -33,3 +33,3 @@ * @class

/**
* Initializes a sanitizers
* Initializes a sanitizer
*

@@ -77,3 +77,3 @@ * @class

// _.set validators and sainitizers as prototype methods on corresponding chains
// _.set validators and sanitizers as prototype methods on corresponding chains
_.forEach(validator, function(method, methodName) {

@@ -155,2 +155,5 @@ if (methodName.match(/^is/) || _.contains(additionalValidators, methodName)) {

req['check' + _.capitalize(location)] = function(param, failMsg) {
if (_.isPlainObject(param)) {
return validateSchema(param, req, location, options);
}
return new ValidatorChain(param, failMsg, req, location, options);

@@ -173,2 +176,5 @@ };

req.check = function(param, failMsg) {
if (_.isPlainObject(param)) {
return validateSchema(param, req, 'any', options);
}
return new ValidatorChain(param, failMsg, req, locate(req, param), options);

@@ -186,2 +192,45 @@ };

/**
* 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} location 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) {
for (var param in schema) {
loc = loc === 'any' ? locate(req, param) : loc;
var validator = new ValidatorChain(param, null, req, loc, options);
var paramErrorMessage = schema[param].errorMessage;
delete schema[param].errorMessage;
for (var methodName in schema[param]) {
validator.failMsg = schema[param][methodName].errorMessage || paramErrorMessage || 'Invalid param';
validator[methodName].apply(validator, schema[param][methodName].options);
}
}
}
/**
* Validates and handles errors, return instance of itself to allow for chaining

@@ -188,0 +237,0 @@ *

2

package.json

@@ -12,3 +12,3 @@ {

],
"version": "2.15.1",
"version": "2.16.0",
"homepage": "https://github.com/ctavan/express-validator",

@@ -15,0 +15,0 @@ "license": "MIT",

@@ -186,2 +186,34 @@ # express-validator

## Validation By schema
Alternatively you can define all your validations at once using a simple schema. This also enables per-validator error messages.
Schema validation will be used if you pass an object to any of the validator methods.
```javascript
req.checkBody({
'email': {
notEmpty: true,
isEmail:
errorMessage: 'Invalid Email'
}
},
'password': {
notEmpty: true,
isLength: {
options: [2, 10] // pass options to the valdatior with the options property as an array
},
errorMessage: 'Invalid Password' // Error message for the parameter
},
'name.first': { //
optional: true, // won't validate if field is empty
isLength: {
options: [2, 10],
errorMessage: 'Must be between 2 and 10 chars long' // Error message for the validator, takes precedent over parameter message
},
errorMessage: 'Invalid First Name'
}
});
```
## Validation errors

@@ -229,3 +261,3 @@

You can provide an error message for a single validation with `.withMessage()`. This can be chained with the rest of your validation, and if you don't use it for one of the validations then it will fall back to the default.
You can provide an error message for a single validation with `.withMessage()`. This can be chained with the rest of your validation, and if you don't use it for one of the validations then it will fall back to the default.

@@ -247,3 +279,3 @@ ```javascript

### Optional input
## Optional input

@@ -250,0 +282,0 @@ You can use the `optional()` method to check an input only when the input exists.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc