Skip to content

Commit

Permalink
Move the ‘Validation.prototype.validateItems’ method into a separate …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
František Hába committed Mar 8, 2012
1 parent 250e5e8 commit f965e3f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/engines/json/validateItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Validation.validateItems
*
* @param {object} instance
* @param {object} schema
* @param {string} path
* @param {function} callback
*/
Validation.prototype.validateItems = function(instance, schema, path, callback) {

// Save a reference to the ‘this’
var self = this;

// If the instance is not empty
if (instance && !isEmpty(instance)) {

/**
* {
* type: 'array',
* items: {
* type: 'object'
* }
* }
* — or —
* {
* type: 'array',
* items: {
* type: 'array'
* }
* }
*/
if (['object', 'array'].indexOf(schema.items.type) !== -1) {
return each(instance, function(index, propertyValue, callback) {

var propertyPath = path + '[' + index + ']';

return self.validateSchema(
propertyValue,
schema.items,
propertyPath,
callback
);

}, callback);

/*
* {
* type: 'array',
* items: {
* type: 'string'
* }
* }
*/
} else {
return each(instance, function(index, propertyValue, callback) {

var propertyPath = path + '[' + index + ']';

return self.validateProperty(
propertyPath,
propertyValue,
schema.items,
callback
);

}, callback);
}

} else {
return callback();
}

};

0 comments on commit f965e3f

Please sign in to comment.