Browse files

Also pass key for validator messages callback

  • Loading branch information...
1 parent a74cf6b commit 0b679b2a7581dfbb5d065fd4b0914436ef4ef2c9 @mendezcode mendezcode committed Feb 14, 2015
Showing with 3 additions and 3 deletions.
  1. +1 −1 lib/validator.js
  2. +2 −2 test/unit/validator.js
View
2 lib/validator.js
@@ -380,7 +380,7 @@ Validator.prototype.validate = function(fields, validOnly) {
// Valid is not valid, send error
// Note: If msg callback doesn't return, the default invalid field msg is used instead
- return messages[key](val) || util.format(this.i18n.invalidField, val);
+ return messages[key](val, key) || util.format(this.i18n.invalidField, val);
}
View
4 test/unit/validator.js
@@ -21,7 +21,7 @@ vows.describe('lib/validator.js').addBatch({
// Default messages
var validator = app.validator({cleanup: true})
- .add({first: /^John Doe$/, last: 'alpha_spaces'}, function(val) { return "cb:error -> " + val;})
+ .add({first: /^John Doe$/, last: 'alpha_spaces'}, function(val, key) { return util.format('cb:error [%s] -> %s', key, val) })
.add({email: 'email'}, "The email is invalid: %s")
.add([{msg: 'alpha'}]) // array arg test for [add]
.addOptional({some: function(val) { return (/^[a-z]$/).test(val); }})
@@ -40,7 +40,7 @@ vows.describe('lib/validator.js').addBatch({
assert.equal(validator.validate({first: "John Doe"}), validator.i18n.missingRequiredFields);
// Returns error msg when value is not validated
- assert.equal(validator.validate({first: "Jane Doe"}), "cb:error -> Jane Doe");
+ assert.equal(validator.validate({first: "Jane Doe"}), "cb:error [first] -> Jane Doe");
// Returns error msg replacing placeholders (%s)
assert.equal(validator.validate({email: 'invalid.email'}), "The email is invalid: invalid.email");

0 comments on commit 0b679b2

Please sign in to comment.