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

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) {
/**
* 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.
*/
this.errors = new sequelizeError.ValidationError('Validation error');
this.errors = [];
/** @type {boolean} Indicates if validations are in progress */
this.inProgress = false;
......@@ -139,15 +139,14 @@ InstanceValidator.prototype.validate = function() {
throw new Error('Validations already in progress.');
}
this.inProgress = true;
this.errors = new sequelizeError.ValidationError('Validation error');
var self = this;
return Promise.settle([
self._builtinValidators(),
self._customValidators()
]).then(function() {
if (Object.keys(self.errors).length) {
return self.errors;
if (self.errors.length) {
return new sequelizeError.ValidationError('Validation error', self.errors);
}
return new Promise(function(resolve) {
......@@ -356,26 +355,14 @@ InstanceValidator.prototype._validateSchema = function(rawAttribute, field, valu
var error;
if (rawAttribute.allowNull === false && ((value === null) || (value === undefined))) {
error = new sequelizeError.ValidationError(field + ' cannot be null');
error.path = field;
error.value = value;
error.type = error.message = 'notNull Violation';
if (!this.errors.hasOwnProperty(field)) {
this.errors[field] = [];
}
this.errors[field].push(error);
error = new sequelizeError.ValidationErrorItem(field + ' cannot be null', 'notNull Violation', field, value);
this.errors.push(error);
}
if (rawAttribute.type === DataTypes.STRING) {
if (Array.isArray(value) || (_.isObject(value) && !value._isSequelizeMethod)) {
error = new sequelizeError.ValidationError(field + ' cannot be an array or an object');
error.path = field;
error.value = value;
error.type = error.message = 'string violation';
if (!this.errors.hasOwnProperty(field)) {
this.errors[field] = [];
}
this.errors[field].push(error);
error = new sequelizeError.ValidationErrorItem(field + ' cannot be an array or an object', 'string violation', field, value);
this.errors.push(error);
}
}
};
......@@ -409,14 +396,9 @@ InstanceValidator.prototype._handleSettledResult = function(field, promiseInspec
* @private
*/
InstanceValidator.prototype._pushError = function(isBuiltin, errorKey, rawError) {
if (!this.errors.hasOwnProperty(errorKey)) {
this.errors[errorKey] = [];
}
var error = new sequelizeError.ValidationError();
var message = rawError.message || rawError || 'Validation error';
var error = new sequelizeError.ValidationErrorItem(message, 'Validation error', errorKey, 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() {
sequelizeErrors.ValidationError;
/**
* Describes a validation error on an instance path
* @property ValidationErrorItem
*/
Sequelize.prototype.ValidationErrorItem = Sequelize.ValidationErrorItem =
sequelizeErrors.ValidationErrorItem;
/**
* Returns 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!