Skip to content

Commit

Permalink
Schema: ignore validator if its value is falsy
Browse files Browse the repository at this point in the history
Closes #263
  • Loading branch information
gustavohenke committed Jul 9, 2017
1 parent 77299b9 commit 817af05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/express_validator.js
Expand Up @@ -217,10 +217,9 @@ var expressValidator = function(options) {
continue;
}

if (methodName === 'errorMessage') {
/* Also do not validate if methodName
* represent parameter error message
*/
if (methodName === 'errorMessage' || !schema[param][methodName]) {
// Also do not validate if methodName represent parameter error message
// or if the value is falsy
continue;
}

Expand Down
20 changes: 20 additions & 0 deletions test/schema.spec.js
Expand Up @@ -96,4 +96,24 @@ describe('Schema validation', () => {
expect(result.mapped()).to.eql({});
});
});

it('ignores validators which values are falsy', () => {
const req = {
body: {
int: 'asd',
upper: 'qwe'
}
};

expressValidator()(req, {}, () => {});
req.check({
int: { isInt: true },
upper: { isUppercase: false }
});

return req.getValidationResult().then(result => {
expect(result.mapped()).to.have.property('int');
expect(result.mapped()).to.not.have.property('upper');
});
});
});

0 comments on commit 817af05

Please sign in to comment.