Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the ‘Validation.prototype.validateProperties’ method into a sepa…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}; |