-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of the ‘additionalProperties’ attribute
- Loading branch information
František Hába
committed
Mar 12, 2012
1 parent
5b393b0
commit 61d1b77
Showing
5 changed files
with
78 additions
and
8 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 |
---|---|---|
@@ -1,10 +1,62 @@ | ||
/** | ||
* AdditionalProperties | ||
*/ | ||
Validation.prototype.addAttributeConstructor('additionalProperties', function additionalPropertiesConstructor() { | ||
return function additionalProperties(property, propertyValue, attributeValue, propertyAttributes, callback) { | ||
|
||
var additionalPropertiesAttribute = function additionalProperties(property, propertyValue, attributeValue, propertyAttributes, callback) { | ||
|
||
var self = this; | ||
|
||
/** | ||
* { | ||
* additionalProperties: true, | ||
* ... | ||
* } | ||
*/ | ||
if (attributeValue === true) { | ||
return callback(); | ||
|
||
}; | ||
}); | ||
} | ||
|
||
// Filter the forbidden properties | ||
var propertyKeys = keys(propertyValue); | ||
var forbiddenProperties = filter(propertyKeys, function(key) { | ||
return !propertyAttributes.properties[key]; | ||
}); | ||
|
||
if (isEmpty(forbiddenProperties)) { | ||
return callback(); | ||
} | ||
|
||
/** | ||
* { | ||
* additionalProperties: false, | ||
* ... | ||
* } | ||
*/ | ||
if (attributeValue === false) { | ||
this.addError(); | ||
return callback(); | ||
} | ||
|
||
/** | ||
* { | ||
* additionalProperties: { | ||
* type: 'string', | ||
* ... | ||
* }, | ||
* ... | ||
* } | ||
*/ | ||
if (isObject(attributeValue)) { | ||
return each(forbiddenProperties, function(index, key, callback) { | ||
return self.validateSchema( | ||
propertyValue[key], | ||
attributeValue, | ||
property + key, | ||
callback | ||
); | ||
}, callback); | ||
} | ||
|
||
}; | ||
|
||
// Export | ||
Validation.prototype.addAttribute('additionalProperties', additionalPropertiesAttribute); |
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
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,9 @@ | ||
/** | ||
* Filter | ||
* | ||
* @param {object} arr | ||
* @param {function} iterator | ||
*/ | ||
var filter = function(arr, iterator, context) { | ||
return Array.prototype.filter.apply(arr, [iterator, context || this]); | ||
}; |
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,8 @@ | ||
/** | ||
* IsDefined | ||
* | ||
* @param {object} input | ||
*/ | ||
var isDefined = function(input) { | ||
return typeof input !== 'undefined'; | ||
}; |
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