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,12 +403,17 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
Model.applyProperties(update, inst);
Model = Model.lookupModel(update);
if (Model.settings.validateUpsert === false) {
update = removeUndefined(update);
self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done);
} else {
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 {
// 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
@ -419,6 +424,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done);
}, update);
}
function done(err, data, info) {
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;
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');