From 385a0ebabd371a683e5e8e4261c84e53f763b31f Mon Sep 17 00:00:00 2001 From: bitmage Date: Tue, 9 Dec 2014 11:51:57 -0700 Subject: [PATCH] validation errors report value of the field that failed --- lib/validations.js | 19 +++++++++++++++---- test/manipulation.test.js | 4 ++-- test/relations.test.js | 6 +++--- test/validations.test.js | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/validations.js b/lib/validations.js index ee371457b..3a6a6ec86 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -735,7 +735,7 @@ function ValidationError(obj) { this.message = util.format( 'The %s instance is not valid. Details: %s.', context ? '`' + context + '`' : 'model', - formatErrors(obj.errors) || '(unknown)' + formatErrors(obj) || '(unknown)' ); this.statusCode = 422; @@ -760,10 +760,21 @@ function ValidationError(obj) { util.inherits(ValidationError, Error); var errorHasStackProperty = !!(new Error).stack; +var inspect = require('util').inspect; + +function formatProperty(obj, propertyName) { + var value = obj[propertyName]; + if ((typeof value) === 'function') + return propertyName + else if ((typeof value) === 'string') + return propertyName + ': ' + inspect(value.substring(0, 32)); + else + return propertyName + ': ' + inspect(value); +} -function formatErrors(errors) { +function formatErrors(obj) { var DELIM = '; '; - errors = errors || {}; + var errors = obj.errors || {}; return Object.getOwnPropertyNames(errors) .filter(function(propertyName) { return Array.isArray(errors[propertyName]); @@ -771,7 +782,7 @@ function formatErrors(errors) { .map(function(propertyName) { var messages = errors[propertyName]; return messages.map(function(msg) { - return '`' + propertyName + '` ' + msg; + return '`' + formatProperty(obj, propertyName) + '` ' + msg; }).join(DELIM); }) .join(DELIM); diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 0f93ae6c8..cb9e10d8f 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -66,7 +66,7 @@ describe('manipulation', function () { it('should not allow user-defined value for the id of object - create', function (done) { Person.create({id: 123456}, function (err, p) { err.should.be.instanceof(ValidationError); - err.message.should.equal('The `Person` instance is not valid. Details: `id` can\'t be set.'); + err.message.should.equal('The `Person` instance is not valid. Details: `id: 123456` can\'t be set.'); err.statusCode.should.equal(422); p.should.be.instanceof(Person); p.id.should.equal(123456); @@ -80,7 +80,7 @@ describe('manipulation', function () { p.isNewRecord().should.be.true; p.save(function(err, inst) { err.should.be.instanceof(ValidationError); - err.message.should.equal('The `Person` instance is not valid. Details: `id` can\'t be set.'); + err.message.should.equal('The `Person` instance is not valid. Details: `id: 123456` can\'t be set.'); err.statusCode.should.equal(422); inst.id.should.equal(123456); inst.isNewRecord().should.be.true; diff --git a/test/relations.test.js b/test/relations.test.js index e9313acc3..85dc9d101 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -2112,7 +2112,7 @@ describe('relations', function () { should.exist(err); err.name.should.equal('ValidationError'); var msg = 'The `Passport` instance is not valid.'; - msg += ' Details: `name` can\'t be blank.'; + msg += ' Details: `name: undefined` can\'t be blank.'; err.message.should.equal(msg); done(); }); @@ -2569,7 +2569,7 @@ describe('relations', function () { Person.create({ name: 'Wilma', addresses: addresses }, function(err, p) { err.name.should.equal('ValidationError'); var expected = 'The `Person` instance is not valid. '; - expected += 'Details: `addresses` contains invalid item: `work` (`street` can\'t be blank).'; + expected += 'Details: `addresses: [ { id: \u001b[32m\'home\'\u001b[39m, street: \u001b[32m\'Home Street\'\u001b[39m },\n { id: \u001b[32m\'work\'\u001b[39m, street: \u001b[32m\'\'\u001b[39m } ]` contains invalid item: `work` (`street` can\'t be blank).'; err.message.should.equal(expected); done(); }); @@ -2769,7 +2769,7 @@ describe('relations', function () { err.name.should.equal('ValidationError'); err.details.codes.street.should.eql(['presence']); var expected = 'The `Address` instance is not valid. '; - expected += 'Details: `street` can\'t be blank.'; + expected += 'Details: `street: undefined` can\'t be blank.'; err.message.should.equal(expected); done(); }); diff --git a/test/validations.test.js b/test/validations.test.js index 11833e101..edf6cb556 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -206,7 +206,7 @@ describe('validations', function () { User.validatesPresenceOf('name'); User.create(function (e, u) { should.exist(e); - e.message.should.match(/`name` can't be blank/); + e.message.should.match(/`name: undefined` can't be blank/); done(); }); });