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

Commit 4e57b889 by overlookmotel

Options passed to validate hooks

1 parent ccda887b
......@@ -165,14 +165,14 @@ InstanceValidator.prototype.validate = function() {
*/
InstanceValidator.prototype.hookValidate = function() {
var self = this;
return self.modelInstance.Model.runHooks('beforeValidate', self.modelInstance).then(function() {
return self.modelInstance.Model.runHooks('beforeValidate', self.modelInstance, self.options).then(function() {
return self.validate().then(function(error) {
if (error) {
throw error;
}
});
}).then(function() {
return self.modelInstance.Model.runHooks('afterValidate', self.modelInstance);
return self.modelInstance.Model.runHooks('afterValidate', self.modelInstance, self.options);
}).return(self.modelInstance);
};
......
......@@ -495,7 +495,10 @@ module.exports = (function() {
return Promise.try(function() {
// Validate
if (options.hooks) {
return self.hookValidate({skip: _.difference(Object.keys(self.rawAttributes), options.fields)});
options.skip = _.difference(Object.keys(self.rawAttributes), options.fields);
return self.hookValidate(options).then(function() {
delete options.skip;
});
}
}).then(function() {
options.fields.forEach(function(field) {
......@@ -683,8 +686,8 @@ module.exports = (function() {
return new InstanceValidator(this, options).validate();
};
Instance.prototype.hookValidate = function(object) {
var validator = new InstanceValidator(this, object);
Instance.prototype.hookValidate = function(options) {
var validator = new InstanceValidator(this, options);
return validator.hookValidate();
};
......
......@@ -1033,7 +1033,9 @@ module.exports = (function() {
var build = self.build(params);
return build.hookValidate({skip: Object.keys(params)}).then(function() {
options.skip = Object.keys(params);
return build.hookValidate(options).then(function() {
delete options.skip;
return Promise.resolve([build, true]);
});
}
......@@ -1193,17 +1195,18 @@ module.exports = (function() {
// Validate
if (options.validate) {
var skippedFields = Utils._.difference(Object.keys(self.attributes), options.fields);
options.skip = Utils._.difference(Object.keys(self.attributes), options.fields);
var errors = [];
return Promise.map(daos, function(dao) {
var fn = options.individualHooks ? 'hookValidate' : 'validate';
return dao[fn]({skip: skippedFields}).then(function(err) {
return dao[fn](options).then(function(err) {
if (!!err) {
errors.push({record: dao, errors: err});
}
});
}).then(function() {
delete options.skip;
if (errors.length) {
return Promise.reject(errors);
}
......@@ -1384,8 +1387,9 @@ module.exports = (function() {
build.set(self._timestampAttributes.updatedAt, attrValueHash[self._timestampAttributes.updatedAt], { raw: true });
// We want to skip validations for all other fields
var skippedFields = Utils._.difference(Object.keys(self.attributes), Object.keys(attrValueHash));
return build.hookValidate({skip: skippedFields}).then(function(attributes) {
options.skip = Utils._.difference(Object.keys(self.attributes), Object.keys(attrValueHash));
return build.hookValidate(options).then(function(attributes) {
delete options.skip;
if (attributes && attributes.dataValues) {
attrValueHash = Utils._.pick(attributes.dataValues, Object.keys(attrValueHash));
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!