Merge pull request #578 from strongloop/fix/validate-upsert

dao: support validateUpsert:false
This commit is contained in:
Miroslav Bajtoš 2015-05-05 17:50:50 +02:00
commit 128665f1bd
2 changed files with 35 additions and 14 deletions

View File

@ -403,22 +403,28 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
Model.applyProperties(update, inst); Model.applyProperties(update, inst);
Model = Model.lookupModel(update); Model = Model.lookupModel(update);
inst.isValid(function(valid) { if (Model.settings.validateUpsert === false) {
if (!valid) {
// TODO(bajtos) Remove validateUpsert option in v3.0
if (Model.settings.validateUpsert) {
return cb(new ValidationError(inst), inst);
} else {
console.warn('Ignoring validation errors in updateOrCreate():');
console.warn(' %s', new ValidationError(inst).message);
// continue with updateOrCreate
}
}
update = removeUndefined(update); update = removeUndefined(update);
self.getDataSource().connector self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done); .updateOrCreate(Model.modelName, update, done);
}, update); } else {
inst.isValid(function(valid) {
if (!valid) {
if (Model.settings.validateUpsert) {
return cb(new ValidationError(inst), inst);
} else {
// TODO(bajtos) Remove validateUpsert:undefined in v3.0
console.warn('Ignoring validation errors in updateOrCreate():');
console.warn(' %s', new ValidationError(inst).message);
// continue with updateOrCreate
}
}
update = removeUndefined(update);
self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done);
}, update);
}
function done(err, data, info) { function done(err, data, info) {
var obj; var obj;

View File

@ -184,7 +184,7 @@ describe('validations', function () {
}); });
}); });
it('should be skipped on upsert by default', function(done) { it('should ignore errors on upsert by default', function(done) {
delete User.validations; delete User.validations;
User.validatesPresenceOf('name'); User.validatesPresenceOf('name');
// It's important to pass an id value, otherwise DAO falls back // It's important to pass an id value, otherwise DAO falls back
@ -192,6 +192,21 @@ describe('validations', function () {
User.updateOrCreate({ id: 999 }, done); User.updateOrCreate({ id: 999 }, done);
}); });
it('should be skipped by upsert when disabled via settings', function(done) {
var Customer = User.extend('Customer');
Customer.attachTo(db);
db.autoupdate(function(err) {
if (err) return done(err);
Customer.prototype.isValid = function() {
throw new Error('isValid() should not be called at all');
};
Customer.settings.validateUpsert = false;
// It's important to pass an id value, otherwise DAO falls back
// to regular create()
Customer.updateOrCreate({ id: 999 }, done);
});
});
it('should work on upsert when enabled via settings', function(done) { it('should work on upsert when enabled via settings', function(done) {
delete User.validations; delete User.validations;
User.validatesPresenceOf('name'); User.validatesPresenceOf('name');