Merge pull request #578 from strongloop/fix/validate-upsert
dao: support validateUpsert:false
This commit is contained in:
commit
128665f1bd
|
@ -403,12 +403,17 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
Model.applyProperties(update, inst);
|
Model.applyProperties(update, inst);
|
||||||
Model = Model.lookupModel(update);
|
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) {
|
inst.isValid(function(valid) {
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
// TODO(bajtos) Remove validateUpsert option in v3.0
|
|
||||||
if (Model.settings.validateUpsert) {
|
if (Model.settings.validateUpsert) {
|
||||||
return cb(new ValidationError(inst), inst);
|
return cb(new ValidationError(inst), inst);
|
||||||
} else {
|
} else {
|
||||||
|
// TODO(bajtos) Remove validateUpsert:undefined in v3.0
|
||||||
console.warn('Ignoring validation errors in updateOrCreate():');
|
console.warn('Ignoring validation errors in updateOrCreate():');
|
||||||
console.warn(' %s', new ValidationError(inst).message);
|
console.warn(' %s', new ValidationError(inst).message);
|
||||||
// continue with updateOrCreate
|
// continue with updateOrCreate
|
||||||
|
@ -419,6 +424,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
self.getDataSource().connector
|
self.getDataSource().connector
|
||||||
.updateOrCreate(Model.modelName, update, done);
|
.updateOrCreate(Model.modelName, update, done);
|
||||||
}, update);
|
}, update);
|
||||||
|
}
|
||||||
|
|
||||||
function done(err, data, info) {
|
function done(err, data, info) {
|
||||||
var obj;
|
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;
|
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');
|
||||||
|
|
Loading…
Reference in New Issue