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.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;
var context = {
@ -2696,14 +2709,17 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
inst = new Model(data, {applyDefaultValues: false});
}
// validation required
inst.isValid(function(valid) {
if (valid) {
if (doValidate === false) {
doUpdate(ctx.where, ctx.data);
} else {
cb(new ValidationError(inst));
// validation required
inst.isValid(function(valid) {
if (!valid) {
return cb(new ValidationError(inst));
}
doUpdate(ctx.where, ctx.data);
}, options);
}
});
});

View File

@ -2259,8 +2259,6 @@ describe('manipulation', function() {
});
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) {
should.exist(err);
err.message.should.equal('Invalid date: notadate');

View File

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