Skip to content

Commit

Permalink
Initial implementation of the ‘additionalProperties’ attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
František Hába committed Mar 12, 2012
1 parent 5b393b0 commit 61d1b77
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
64 changes: 58 additions & 6 deletions src/engines/json/attributes/additionalProperties.js
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);
2 changes: 1 addition & 1 deletion src/engines/json/validateProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Validation.prototype.validateProperty = function(property, propertyValue, proper
}
};

if (propertyAttributes[attributeName]) {
if (isDefined(propertyAttributes[attributeName])) {
return attributeFn.apply(context, [
property,
propertyValue,
Expand Down
9 changes: 9 additions & 0 deletions src/utils/filter.js
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]);
};
8 changes: 8 additions & 0 deletions src/utils/isDefined.js
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';
};
3 changes: 2 additions & 1 deletion tools/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ var files = [
'./src/utils/detectType.js',
'./src/utils/each.js',
'./src/utils/every.js',
'./src/utils/filter.js',
'./src/utils/hasProperty.js',
'./src/utils/isArray.js',
'./src/utils/isBoolean.js',
'./src/utils/isDefined.js',
'./src/utils/isEmpty.js',
'./src/utils/isFunction.js',
'./src/utils/isInteger.js',
Expand All @@ -39,7 +41,6 @@ var files = [
'./src/engines/json/addAttribute.js',
'./src/engines/json/addAttributeConstructor.js',

'./src/engines/json/attributes/additionalItems.js',
'./src/engines/json/attributes/additionalProperties.js',
'./src/engines/json/attributes/divisibleBy.js',
'./src/engines/json/attributes/enum.js',
Expand Down

0 comments on commit 61d1b77

Please sign in to comment.