Skip to content

Commit

Permalink
Move the ‘Validation.prototype.validateProperties’ method into a sepa…
Browse files Browse the repository at this point in the history
…rate file
  • Loading branch information
František Hába committed Mar 8, 2012
1 parent f965e3f commit d1048b1
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 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.