express-validator
Advanced tools
Comparing version 2.17.0 to 2.17.1
@@ -0,0 +0,0 @@ ## Change Log |
@@ -107,3 +107,3 @@ var validator = require('validator'); | ||
this.req._validationErrors.pop(); | ||
var error = this.errorFormatter(this.lastError.param, message, this.lastError.value); | ||
var error = formatErrors.call(this, this.lastError.param, message, this.lastError.value); | ||
this.validationErrors.push(error); | ||
@@ -273,5 +273,4 @@ this.req._validationErrors.push(error); | ||
var isValid = container[methodName].apply(container, args); | ||
var error = formatErrors.call(this, this.param, this.failMsg || 'Invalid value', this.value); | ||
var error = this.errorFormatter(this.param, this.failMsg || 'Invalid value', this.value); | ||
if (isValid.then) { | ||
@@ -345,3 +344,45 @@ var promise = isValid.catch(function() { | ||
/** | ||
* format param output if passed in as array (for nested) | ||
* before calling errorFormatter | ||
* | ||
* @method param | ||
* @param {(string|string[])} param parameter as a string or array | ||
* @param {string} msg | ||
* @param {string} value | ||
* @return {function} | ||
*/ | ||
function formatErrors(param, msg, value) { | ||
var formattedParam = formatParamOutput(param); | ||
return this.errorFormatter(formattedParam, msg, value); | ||
} | ||
// Convert nested params as array into string for output | ||
// Ex: ['users', '0', 'fields', 'email'] to 'users[0].fields.email' | ||
function formatParamOutput(param) { | ||
if (Array.isArray(param)) { | ||
param = param.reduce(function(prev, curr) { | ||
var part = ''; | ||
if (validator.isInt(curr)) { | ||
part = '[' + curr + ']'; | ||
} else { | ||
if (prev) { | ||
part = '.' + curr; | ||
} else { | ||
part = curr; | ||
} | ||
} | ||
return prev + part; | ||
}); | ||
} | ||
return param; | ||
} | ||
module.exports = expressValidator; | ||
module.exports.validator = validator; | ||
module.exports.utils = { | ||
formatParamOutput: formatParamOutput | ||
}; |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "2.17.0", | ||
"version": "2.17.1", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -15,0 +15,0 @@ "license": "MIT", |
@@ -205,4 +205,2 @@ # express-validator | ||
... | ||
req.check('username', 'Username Taken').isUsernameAvailable(); | ||
@@ -214,35 +212,34 @@ | ||
``` | ||
## Validation By schema | ||
``` | ||
## 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. | ||
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' | ||
} | ||
}); | ||
``` | ||
```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 | ||
@@ -287,2 +284,3 @@ | ||
``` | ||
*Note: Using mappedErrors will only provide the last error per param in the chain of validation errors.* | ||
@@ -289,0 +287,0 @@ ### Per-validation messages |
@@ -11,2 +11,3 @@ var chai = require('chai'); | ||
req.assert(['user', 'fields', 'email'], 'valid email required').isEmail(); | ||
req.assert(['admins', '0', 'name'], 'must only contain letters').isAlpha(); | ||
@@ -24,2 +25,8 @@ var errors = req.validationErrors(); | ||
expect(body[2]).to.have.property('msg', 'valid email required'); | ||
// Should convert ['user', 'fields', 'email'] to 'user.fields.email' | ||
// when formatting the error output | ||
expect(body[0]).to.have.property('param').and.to.be.a('string'); | ||
expect(body[1]).to.have.property('param').and.to.be.a('string'); | ||
expect(body[2]).to.have.property('param').and.to.be.a('string'); | ||
} | ||
@@ -48,11 +55,10 @@ | ||
// TODO: This functionality should probably be easier to test as a unit | ||
describe('nested input as array or dot notation', function() { | ||
it('should return a success when the correct data is passed on the body', function(done) { | ||
testRoute('/', { user: { fields: { email: 'test@example.com' } } }, pass, done); | ||
testRoute('/', { user: { fields: { email: 'test@example.com' } }, admins: [{ name: 'Bobby' }] }, pass, done); | ||
}); | ||
it('should return an error object with each failing param as a property data is invalid', function(done) { | ||
testRoute('/', { user: { fields: { email: '' } } }, fail, done); | ||
testRoute('/', { user: { fields: { email: '' } }, admins: [{ name: 0 }] }, fail, done); | ||
}); | ||
}); |
@@ -51,3 +51,3 @@ var chai = require('chai'); | ||
// TODO: Don't know if all of these are necessary, but we do need to test body and header | ||
describe('#optional()', function() { | ||
describe('#optionalSchema()', function() { | ||
it('should return a success when there is an empty route', function(done) { | ||
@@ -54,0 +54,0 @@ testRoute('/', pass, done); |
@@ -59,3 +59,3 @@ var chai = require('chai'); | ||
describe('#sanitizers', function() { | ||
describe('#sanitizers (check results)', function() { | ||
describe('GET tests', function() { | ||
@@ -62,0 +62,0 @@ it('should return property and sanitized value when param is present', function(done) { |
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
112305
35
1899
371