Merge pull request #578 from strongloop/fix/validate-upsert
dao: support validateUpsert:false
This commit is contained in:
commit
128665f1bd
32
lib/dao.js
32
lib/dao.js
|
@ -403,22 +403,28 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
|||
Model.applyProperties(update, inst);
|
||||
Model = Model.lookupModel(update);
|
||||
|
||||
inst.isValid(function(valid) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if (Model.settings.validateUpsert === false) {
|
||||
update = removeUndefined(update);
|
||||
self.getDataSource().connector
|
||||
.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) {
|
||||
var obj;
|
||||
|
|
|
@ -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;
|
||||
User.validatesPresenceOf('name');
|
||||
// It's important to pass an id value, otherwise DAO falls back
|
||||
|
@ -192,6 +192,21 @@ describe('validations', function () {
|
|||
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) {
|
||||
delete User.validations;
|
||||
User.validatesPresenceOf('name');
|
||||
|
|
Loading…
Reference in New Issue