Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move the ‘Validation.prototype.validateProperty’ method into a separa…

…te file
  • Loading branch information...
commit f14c194d92a661d78cfc4f37cf55338be30d34f7 1 parent d1048b1
@Baggz authored
Showing with 74 additions and 0 deletions.
  1. +74 −0 src/engines/json/validateProperty.js
View
74 src/engines/json/validateProperty.js
@@ -0,0 +1,74 @@
+/**
+ * Validation.validateProperty
+ *
+ * @param {string} property
+ * @param {object} propertyAttributes
+ * @param {string|object} propertyValue
+ * @param {boolean} singleError
+ * @param {function} callback
+ */
+Validation.prototype.validateProperty = function(property, propertyValue, propertyAttributes, callback) {
+
+ // Save a reference to the ‘this’
+ var self = this;
+
+ /**
+ * Iterator
+ *
+ * @param {string} attributeName
+ * @param {function} callback
+ */
+ var iterator = function(attributeName, attributeFn, callback) {
+
+ /**
+ * OnComplete
+ *
+ * @param {object} error
+ */
+ var onComplete = function(error) {
+
+ if (!error) return callback();
+
+ // Renders an error messaage
+ var errorMessage = self.renderErrorMessage(attributeName, {
+ property: property,
+ propertyValue: propertyValue,
+ attribute: propertyAttributes[attributeName]
+ });
+
+ // Add a new error
+ self.Errors.addError({
+ property: property,
+ propertyValue: propertyValue,
+ attribute: attributeName,
+ attributeValue: propertyAttributes[attributeName],
+ message: errorMessage
+ });
+
+ // If the ‘singleError’ is on, stop the validation process
+ return callback(self.singleError ? true : null);
+
+ };
+
+ if (propertyAttributes[attributeName]) {
+ return attributeFn(
+ property,
+ propertyValue,
+ propertyAttributes[attributeName],
+ propertyAttributes,
+ onComplete
+ );
+ } else {
+ return callback();
+ }
+
+ };
+
+ // If it's not a required param and it's empty, skip
+ if (propertyAttributes.required !== true && typeof propertyValue === 'undefined') {
+ return callback();
+ } else {
+ return each(self.attributes, iterator, callback);
+ }
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.