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

Commit 4e57b889 by overlookmotel

Options passed to validate hooks

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