diff --git a/lib/dao.js b/lib/dao.js index c454d758..869e9c50 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1978,7 +1978,12 @@ DataAccessObject.find = function find(query, options, cb) { applySetters: false, persisted: true, }; - var obj = new Model(data, ctorOpts); + var obj; + try { + obj = new Model(data, ctorOpts); + } catch (err) { + return cb(err); + } if (query && query.include) { if (query.collect) { @@ -3173,7 +3178,11 @@ function(data, options, cb) { } // update instance's properties - inst.setAttributes(data); + try { + inst.setAttributes(data); + } catch (err) { + return cb(err); + } if (doValidate) { inst.isValid(function(valid) { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 6f550ceb..9d0f0762 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -685,6 +685,15 @@ describe('manipulation', function() { }); }); + it('should fail if field validation fails', function(done) { + person.updateAttributes({'name': 'John', dob: 'notadate'}, + function(err, p) { + should.exist(err); + err.message.should.equal('Invalid date: notadate'); + done(); + }); + }); + it('should allow model instance on updateAttributes', function(done) { person.updateAttributes(new Person({'name': 'John', age: undefined}), function(err, p) {