Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial implementation of the ‘additionalProperties’ attribute

  • Loading branch information...
commit 61d1b7748efbec9b1cf3d54c03d4e0affb60a1da 1 parent 5b393b0
@Baggz authored
View
64 src/engines/json/attributes/additionalProperties.js
@@ -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);
View
2  src/engines/json/validateProperty.js
@@ -59,7 +59,7 @@ Validation.prototype.validateProperty = function(property, propertyValue, proper
}
};
- if (propertyAttributes[attributeName]) {
+ if (isDefined(propertyAttributes[attributeName])) {
return attributeFn.apply(context, [
property,
propertyValue,
View
9 src/utils/filter.js
@@ -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]);
+};
View
8 src/utils/isDefined.js
@@ -0,0 +1,8 @@
+/**
+ * IsDefined
+ *
+ * @param {object} input
+ */
+var isDefined = function(input) {
+ return typeof input !== 'undefined';
+};
View
3  tools/compile.js
@@ -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',
@@ -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',
Please sign in to comment.
Something went wrong with that request. Please try again.