Browse files

Move the ‘Validation.prototype.validateProperties’ method into a sepa…

…rate file
  • Loading branch information...
1 parent f965e3f commit d1048b1ac5eaf3b49853bcd95d78885394f5c522 @Baggz committed Mar 8, 2012
Showing with 58 additions and 0 deletions.
  1. +58 −0 src/engines/json/validateProperties.js
View
58 src/engines/json/validateProperties.js
@@ -0,0 +1,58 @@
+/**
+ * Validation.validateProperties
+ *
+ * @param {object} instance
+ * @param {object} schema
+ * @param {string} path
+ * @param {function} callback
+ */
+Validation.prototype.validateProperties = function(instance, schema, path, callback) {
+
+ // Save a reference to the ‘this’
+ var self = this;
+
+ // Goes
+ return each(schema.properties, function(property, propertyAttributes, callback) {
+
+ var isObject = propertyAttributes.type === 'object' && propertyAttributes.properties,
+ isArray = propertyAttributes.type === 'array';
+
+ // Get the value of property (instance[property])
+ var propertyValue = self.getProperty(instance, property);
+
+ // Compose the property path
+ var propertyName = property.indexOf(' ') !== -1 ? '[\'' + property + '\']' : '.' + property,
+ propertyPath = path.length === 0 ? property : path + propertyName;
+
+ /**
+ * {
+ * type: 'object',
+ * properties: {
+ * user: {
+ * type: 'object',
+ * properties: {
+ * ...
+ * }
+ * }
+ * }
+ * }
+ */
+ if (isObject || isArray) {
+ return self.validateSchema(
+ propertyValue,
+ schema.properties[property],
+ propertyPath,
+ callback
+ );
+ } else {
+ return self.validateProperty(
+ propertyPath,
+ propertyValue,
+ propertyAttributes,
+ callback
+ );
+ }
+
+ }, callback);
+
+};

0 comments on commit d1048b1

Please sign in to comment.