Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Expose only certain methods from the validation process

  • Loading branch information...
commit cf2eb9736b6ad8d71c7d9aa6be003c254450096c 1 parent 0fdf129
@Baggz authored
Showing with 36 additions and 28 deletions.
  1. +36 −28 src/engines/json/validateProperty.js
View
64 src/engines/json/validateProperty.js
@@ -1,7 +1,7 @@
/**
* Validation.validateProperty
*
- * @param {string} property
+ * @param {string} propertyName
* @param {object} propertyAttributes
* @param {string|object} propertyValue
* @param {boolean} singleError
@@ -12,52 +12,59 @@ Validation.prototype.validateProperty = function(property, propertyValue, proper
// Save a reference to the ‘this’
var self = this;
+ var context = {};
+
+ [
+ 'validateItems',
+ 'validateProperties',
+ 'validateSchema',
+ 'validateProperty',
+ 'getProperty'
+ ].forEach(function(key) {
+ context[key] = this[key];
+ }, self);
+
/**
* Iterator
*
* @param {string} attributeName
+ * @param {function} attributeFn
* @param {function} callback
*/
var iterator = function(attributeName, attributeFn, callback) {
- /**
- * OnComplete
- *
- * @param {object} error
- */
- var onComplete = function(error) {
-
- if (!error) return callback();
+ var lastLength = self.errors.length;
- // Renders an error messaage
- var errorMessage = self.renderErrorMessage(attributeName, {
+ // Overwrite the ‘addError’ method
+ context.addError = function(message) {
+ return self.errors.push({
property: property,
propertyValue: propertyValue,
- attribute: propertyAttributes[attributeName]
- });
-
- // Add a new error
- self.Errors.addError({
- property: property,
- propertyValue: propertyValue,
- attribute: attributeName,
+ attributeName: attributeName,
attributeValue: propertyAttributes[attributeName],
- message: errorMessage
+ message: message
});
+ };
- // If the ‘singleError’ is on, stop the validation process
- return callback(self.singleError ? true : null);
-
+ /**
+ * OnComplete
+ */
+ var onComplete = function() {
+ if (self.errors.length > lastLength && self.singleError) {
+ return callback(true);
+ } else {
+ return callback();
+ }
};
if (propertyAttributes[attributeName]) {
- return attributeFn(
+ return attributeFn.apply(context, [
property,
propertyValue,
propertyAttributes[attributeName],
propertyAttributes,
onComplete
- );
+ ]);
} else {
return callback();
}
@@ -65,10 +72,11 @@ Validation.prototype.validateProperty = function(property, propertyValue, proper
};
// If it's not a required param and it's empty, skip
- if (propertyAttributes.required !== true && typeof propertyValue === 'undefined') {
+ if (propertyAttributes.required !== true && isUndefined(propertyValue)) {
return callback();
- } else {
- return each(self.attributes, iterator, callback);
}
+
+ // Validate the property
+ return each(self.attributes, iterator, callback);
};
Please sign in to comment.
Something went wrong with that request. Please try again.