Merge pull request #1451 from strongloop/backwards-compatability-validate-update

Honor backwards compatibility with validate update
This commit is contained in:
Sakib Hasan 2017-08-03 13:43:01 -04:00 committed by GitHub
commit c813bf19fb
3 changed files with 25 additions and 9 deletions

View File

@ -2669,6 +2669,19 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
this.applyScope(query); this.applyScope(query);
this.applyProperties(data); this.applyProperties(data);
var doValidate = false;
if (options.validate === undefined) {
if (Model.settings.validateUpdate === undefined) {
if (Model.settings.automaticValidation !== undefined) {
doValidate = Model.settings.automaticValidation;
}
} else {
doValidate = Model.settings.validateUpdate;
}
} else {
doValidate = options.validate;
}
where = query.where; where = query.where;
var context = { var context = {
@ -2696,14 +2709,17 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
inst = new Model(data, {applyDefaultValues: false}); inst = new Model(data, {applyDefaultValues: false});
} }
// validation required if (doValidate === false) {
inst.isValid(function(valid) { doUpdate(ctx.where, ctx.data);
if (valid) { } else {
// validation required
inst.isValid(function(valid) {
if (!valid) {
return cb(new ValidationError(inst));
}
doUpdate(ctx.where, ctx.data); doUpdate(ctx.where, ctx.data);
} else { }, options);
cb(new ValidationError(inst)); }
}
}, options);
}); });
}); });

View File

@ -2259,8 +2259,6 @@ describe('manipulation', function() {
}); });
it('should not coerce invalid values provided in where conditions', function(done) { it('should not coerce invalid values provided in where conditions', function(done) {
// remove the validations since update/updateAll validates the model
delete Person.validations;
Person.update({name: 'Brett Boe'}, {dob: 'notadate'}, function(err) { Person.update({name: 'Brett Boe'}, {dob: 'notadate'}, function(err) {
should.exist(err); should.exist(err);
err.message.should.equal('Invalid date: notadate'); err.message.should.equal('Invalid date: notadate');

View File

@ -51,6 +51,8 @@ describe('validations', function() {
id: {type: Number, id: true, generated: false}, id: {type: Number, id: true, generated: false},
name: {type: String}, name: {type: String},
age: {type: Number}, age: {type: Number},
}, {
validateUpdate: true,
}); });
Entry.validatesUniquenessOf('id'); Entry.validatesUniquenessOf('id');
db.automigrate(function(err) { db.automigrate(function(err) {