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

Commit f0c01eda by Brian Romanko

Updating the instance validator errors to use the new ValidationErrorItem

1 parent 8be24f37
Showing with 18 additions and 29 deletions
...@@ -115,10 +115,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) { ...@@ -115,10 +115,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
/** /**
* All errors will be stored here from the validations. * All errors will be stored here from the validations.
* *
* @type {Sequelize.error} Will contain keys that correspond to attributes which will * @type {Array} Will contain keys that correspond to attributes which will
* be Arrays of Errors. * be Arrays of Errors.
*/ */
this.errors = new sequelizeError.ValidationError('Validation error'); this.errors = [];
/** @type {boolean} Indicates if validations are in progress */ /** @type {boolean} Indicates if validations are in progress */
this.inProgress = false; this.inProgress = false;
...@@ -139,15 +139,14 @@ InstanceValidator.prototype.validate = function() { ...@@ -139,15 +139,14 @@ InstanceValidator.prototype.validate = function() {
throw new Error('Validations already in progress.'); throw new Error('Validations already in progress.');
} }
this.inProgress = true; this.inProgress = true;
this.errors = new sequelizeError.ValidationError('Validation error');
var self = this; var self = this;
return Promise.settle([ return Promise.settle([
self._builtinValidators(), self._builtinValidators(),
self._customValidators() self._customValidators()
]).then(function() { ]).then(function() {
if (Object.keys(self.errors).length) { if (self.errors.length) {
return self.errors; return new sequelizeError.ValidationError('Validation error', self.errors);
} }
return new Promise(function(resolve) { return new Promise(function(resolve) {
...@@ -356,26 +355,14 @@ InstanceValidator.prototype._validateSchema = function(rawAttribute, field, valu ...@@ -356,26 +355,14 @@ InstanceValidator.prototype._validateSchema = function(rawAttribute, field, valu
var error; var error;
if (rawAttribute.allowNull === false && ((value === null) || (value === undefined))) { if (rawAttribute.allowNull === false && ((value === null) || (value === undefined))) {
error = new sequelizeError.ValidationError(field + ' cannot be null'); error = new sequelizeError.ValidationErrorItem(field + ' cannot be null', 'notNull Violation', field, value);
error.path = field; this.errors.push(error);
error.value = value;
error.type = error.message = 'notNull Violation';
if (!this.errors.hasOwnProperty(field)) {
this.errors[field] = [];
}
this.errors[field].push(error);
} }
if (rawAttribute.type === DataTypes.STRING) { if (rawAttribute.type === DataTypes.STRING) {
if (Array.isArray(value) || (_.isObject(value) && !value._isSequelizeMethod)) { if (Array.isArray(value) || (_.isObject(value) && !value._isSequelizeMethod)) {
error = new sequelizeError.ValidationError(field + ' cannot be an array or an object'); error = new sequelizeError.ValidationErrorItem(field + ' cannot be an array or an object', 'string violation', field, value);
error.path = field; this.errors.push(error);
error.value = value;
error.type = error.message = 'string violation';
if (!this.errors.hasOwnProperty(field)) {
this.errors[field] = [];
}
this.errors[field].push(error);
} }
} }
}; };
...@@ -409,14 +396,9 @@ InstanceValidator.prototype._handleSettledResult = function(field, promiseInspec ...@@ -409,14 +396,9 @@ InstanceValidator.prototype._handleSettledResult = function(field, promiseInspec
* @private * @private
*/ */
InstanceValidator.prototype._pushError = function(isBuiltin, errorKey, rawError) { InstanceValidator.prototype._pushError = function(isBuiltin, errorKey, rawError) {
if (!this.errors.hasOwnProperty(errorKey)) { var message = rawError.message || rawError || 'Validation error';
this.errors[errorKey] = []; var error = new sequelizeError.ValidationErrorItem(message, 'Validation error', errorKey, rawError);
}
var error = new sequelizeError.ValidationError();
error[InstanceValidator.RAW_KEY_NAME] = rawError; error[InstanceValidator.RAW_KEY_NAME] = rawError;
error.message = rawError.message || rawError || 'Validation error';
this.errors[errorKey].push(error); this.errors.push(error);
}; };
...@@ -238,6 +238,13 @@ module.exports = (function() { ...@@ -238,6 +238,13 @@ module.exports = (function() {
sequelizeErrors.ValidationError; sequelizeErrors.ValidationError;
/** /**
* Describes a validation error on an instance path
* @property ValidationErrorItem
*/
Sequelize.prototype.ValidationErrorItem = Sequelize.ValidationErrorItem =
sequelizeErrors.ValidationErrorItem;
/**
* Returns the specified dialect. * Returns the specified dialect.
* *
* @return {String} The specified dialect. * @return {String} The specified dialect.
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!