diff --git a/lib/validations.js b/lib/validations.js index bcf6a805..9cd368f9 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -506,7 +506,7 @@ function validationFailed(inst, v, cb) { validatorArguments.push(attr); validatorArguments.push(conf); validatorArguments.push(function onerror(kind) { - var message, code = conf.validation; + var message, code = conf.code || conf.validation; if (conf.message) { message = conf.message; } diff --git a/test/validations.test.js b/test/validations.test.js index 1a3faf55..935a2669 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -295,21 +295,23 @@ describe('validations', function () { it('should validate using custom sync validation', function() { User.validate('email', function (err) { if (this.email === 'hello') err(); - }); + }, { code: 'invalid-email' }); var u = new User({email: 'hello'}); Boolean(u.isValid()).should.be.false; + u.errors.codes.should.eql({ email: ['invalid-email'] }); }); it('should validate and return detailed error messages', function() { User.validate('global', function (err) { if (this.email === 'hello' || this.email === 'hey') { - this.errors.add('hello', 'Cannot be `' + this.email + '`', 'invalid'); + this.errors.add('email', 'Cannot be `' + this.email + '`', 'invalid-email'); err(false); // false: prevent global error message } }); var u = new User({email: 'hello'}); Boolean(u.isValid()).should.be.false; - u.errors.should.eql({ hello: [ 'Cannot be `hello`' ] }); + u.errors.should.eql({ email: ['Cannot be `hello`'] }); + u.errors.codes.should.eql({ email: ['invalid-email'] }); }); it('should validate using custom async validation', function(done) {