不要怂,就是干,撸起袖子干!

Commit 191819fa by Aaron Williams Committed by Sushant

fix(model/upsert): support options.validation (#8872)

1 parent 60c04d97
...@@ -2233,7 +2233,8 @@ class Model { ...@@ -2233,7 +2233,8 @@ class Model {
static upsert(values, options) { static upsert(values, options) {
options = Object.assign({ options = Object.assign({
hooks: true, hooks: true,
returning: false returning: false,
validate: true
}, Utils.cloneDeep(options || {})); }, Utils.cloneDeep(options || {}));
options.model = this; options.model = this;
...@@ -2247,7 +2248,11 @@ class Model { ...@@ -2247,7 +2248,11 @@ class Model {
options.fields = Object.keys(instance._changed); options.fields = Object.keys(instance._changed);
} }
return instance.validate(options).then(() => { return Promise.try(() => {
if (options.validate) {
return instance.validate(options);
}
}).then(() => {
// Map field names // Map field names
const updatedDataValues = _.pick(instance.dataValues, Object.keys(instance._changed)); const updatedDataValues = _.pick(instance.dataValues, Object.keys(instance._changed));
const insertValues = Utils.mapValueFieldNames(instance.dataValues, instance.attributes, this); const insertValues = Utils.mapValueFieldNames(instance.dataValues, instance.attributes, this);
......
...@@ -196,6 +196,29 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -196,6 +196,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return expect(User.upsert({ email: 'notanemail' })).to.eventually.be.rejectedWith(this.sequelize.ValidationError); return expect(User.upsert({ email: 'notanemail' })).to.eventually.be.rejectedWith(this.sequelize.ValidationError);
}); });
it('supports skipping validations', function() {
const User = this.sequelize.define('user', {
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
}
});
const options = { validate: false };
return User.sync({ force: true })
.then(() => User.upsert({ id: 1, email: 'notanemail' }, options))
.then(created => {
if (dialect === 'sqlite') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.okay;
}
});
});
it('works with BLOBs', function() { it('works with BLOBs', function() {
return this.User.upsert({ id: 42, username: 'john', blob: new Buffer('kaj') }).bind(this).then(function(created) { return this.User.upsert({ id: 42, username: 'john', blob: new Buffer('kaj') }).bind(this).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!