express-validator
Advanced tools
Comparing version 2.17.1 to 2.18.0
@@ -0,0 +0,0 @@ ## Change Log |
@@ -95,5 +95,20 @@ var validator = require('validator'); | ||
ValidatorChain.prototype.optional = function() { | ||
if (this.value === undefined) { | ||
this.skipValidating = true; | ||
ValidatorChain.prototype.optional = function(opts) { | ||
opts = opts || {}; | ||
// By default, optional checks if the key exists, but the user can pass in | ||
// checkFalsy: true to skip validation if the property is falsy | ||
var defaults = { | ||
checkFalsy: false | ||
}; | ||
var options = _.assign(defaults, opts); | ||
if (options.checkFalsy) { | ||
if (!this.value) { | ||
this.skipValidating = true; | ||
} | ||
} else { | ||
if (this.value === undefined) { | ||
this.skipValidating = true; | ||
} | ||
} | ||
@@ -170,2 +185,10 @@ | ||
req.sanitizeHeaders = function(param) { | ||
if (param === 'referrer') { | ||
param = 'referer'; | ||
} | ||
return new Sanitizer(param, req, ['headers']); | ||
}; | ||
req.sanitize = function(param) { | ||
@@ -172,0 +195,0 @@ return new Sanitizer(param, req, locations); |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "2.17.1", | ||
"version": "2.18.0", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -34,3 +34,3 @@ "license": "MIT", | ||
"lodash": "3.10.x", | ||
"validator": "4.0.x" | ||
"validator": "4.2.x" | ||
}, | ||
@@ -37,0 +37,0 @@ "devDependencies": { |
@@ -169,2 +169,4 @@ # express-validator | ||
If a validator takes in params, you would call it like `req.assert('reqParam').contains('thisString');`. | ||
Validators are appended and can be chained. See [chriso/validator.js](https://github.com/chriso/validator.js) for available validators, or [add your own](#customvalidators). | ||
@@ -187,2 +189,5 @@ | ||
#### req.checkHeaders(); | ||
Only checks `req.headers`. This method is not covered by the general `req.check()`. | ||
## Asynchronous Validation | ||
@@ -222,4 +227,4 @@ | ||
'email': { | ||
notEmpty: true, | ||
isEmail: | ||
notEmpty: true, | ||
isEmail: { | ||
errorMessage: 'Invalid Email' | ||
@@ -231,3 +236,3 @@ } | ||
isLength: { | ||
options: [2, 10] // pass options to the valdatior with the options property as an array | ||
options: [2, 10] // pass options to the validator with the options property as an array | ||
}, | ||
@@ -309,3 +314,3 @@ errorMessage: 'Invalid Password' // Error message for the parameter | ||
You can use the `optional()` method to check an input only when the input exists. | ||
You can use the `optional()` method to skip validation. By default, it only skips validation if the key does not exist on the request object. If you want to skip validation based on the property being falsy (null, undefined, etc), you can pass in `{ checkFalsy: true }`. | ||
@@ -335,2 +340,4 @@ ```javascript | ||
If a sanitizer takes in params, you would call it like `req.sanitize('reqParam').whitelist(['a', 'b', 'c']);`. | ||
If the parameter is present in multiple places with the same name e.g. `req.params.comment` & `req.query.comment`, they will all be sanitized. | ||
@@ -350,2 +357,5 @@ | ||
#### req.sanitizeHeaders(); | ||
Only sanitizes `req.headers`. This method is not covered by the general `req.sanitize()`. | ||
### Regex routes | ||
@@ -352,0 +362,0 @@ |
@@ -18,2 +18,13 @@ var chai = require('chai'); | ||
req.assert({ | ||
'optional_falsy_param': { | ||
optional: { | ||
options: [{ checkFalsy: true }] | ||
}, | ||
isInt: { | ||
errorMessage: errorMessage | ||
} | ||
} | ||
}); | ||
var errors = req.validationErrors(); | ||
@@ -80,2 +91,10 @@ if (errors) { | ||
}); | ||
it('should return a success when the optional falsy param is present, but false', function(done) { | ||
testRoute('/path?optional_falsy_param=', pass, done); | ||
}); | ||
it('should return an error when the optional falsy param is present, but does not pass', function(done) { | ||
testRoute('/path?optional_falsy_param=hello', fail, done); | ||
}); | ||
}); |
@@ -10,2 +10,3 @@ var chai = require('chai'); | ||
req.assert('optional_param', errorMessage).optional().isInt(); | ||
req.assert('optional_falsy_param', errorMessage).optional({ checkFalsy: true }).isInt(); | ||
@@ -73,2 +74,10 @@ var errors = req.validationErrors(); | ||
}); | ||
it('should return a success when the optional falsy param is present, but false', function(done) { | ||
testRoute('/path?optional_falsy_param=', pass, done); | ||
}); | ||
it('should return an error when the optional falsy param is present, but does not pass', function(done) { | ||
testRoute('/path?optional_falsy_param=hello', fail, done); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
116025
36
1980
381
+ Addedvalidator@4.2.1(transitive)
- Removedvalidator@4.0.6(transitive)
Updatedvalidator@4.2.x