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

Commit 6457f3bb by Mick Hansen

fix/feat(instance): skip validations with {validate: false}

1 parent b50934db
Showing with 23 additions and 6 deletions
......@@ -445,6 +445,7 @@ module.exports = (function() {
* @param {Object} [options]
* @param {Object} [options.fields] An optional array of strings, representing database columns. If fields is provided, only those columns will be validation and saved.
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Boolean} [options.validate=true] If false, validations won't be run.
* @param {Transaction} [options.transaction]
*
* @return {Promise<this|Errors.ValidationError>}
......@@ -455,7 +456,8 @@ module.exports = (function() {
}
options = Utils._.extend({
hooks: true
hooks: true,
validate: true
}, options, deprecated);
if (!options.fields) {
......@@ -497,9 +499,9 @@ module.exports = (function() {
return Promise.try(function() {
// Validate
if (options.hooks) {
if (options.validate) {
options.skip = _.difference(Object.keys(self.rawAttributes), options.fields);
return self.hookValidate(options).then(function() {
return (options.hooks ? self.hookValidate(options) : self.validate(options)).then(function() {
delete options.skip;
});
}
......@@ -686,9 +688,7 @@ module.exports = (function() {
};
Instance.prototype.hookValidate = function(options) {
var validator = new InstanceValidator(this, options);
return validator.hookValidate();
return new InstanceValidator(this, options).hookValidate();
};
/**
......
......@@ -746,6 +746,23 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
})
})
it('skips validation when asked', function() {
var values = ['value1', 'value2'];
var Bar = this.sequelize.define('Bar' + config.rand(), {
field: {
type: Sequelize.ENUM,
values: values,
validate: {
isIn: [values]
}
}
});
return Bar.sync({force: true}).then(function () {
return Bar.create({ field: 'value3' }, {validate: false});
});
});
it("raises an error if saving a different value into an immutable field", function(done) {
var User = this.sequelize.define('User', {
name: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!