jsonschema
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -118,2 +118,3 @@ 'use strict'; | ||
if (helpers.isDefined(schema.properties)) { | ||
this.validateAdditionalProperties(instance, schema, options); | ||
var keys = Object.keys(schema.properties); | ||
@@ -134,2 +135,22 @@ var keysLength = keys.length; | ||
Validator.prototype.validateAdditionalProperties = function (instance, schema, options) { | ||
if (!helpers.isDefined(schema.additionalProperties) || schema.additionalProperties === true) { | ||
return; | ||
} | ||
var property, i; | ||
var keys = Object.keys(instance); | ||
var keysLength = keys.length; | ||
for (i = 0; i < keysLength; i++) { | ||
property = keys[i]; | ||
if (!helpers.isDefined(schema.properties[property])) { | ||
if (schema.additionalProperties === false) { | ||
this.addError("Property " + property + " does not exist in the schema"); | ||
} else { | ||
this.validate(instance[property], schema.additionalProperties, options); | ||
} | ||
} | ||
} | ||
}; | ||
// FIXME: This will fail if the schema doesn't have 'items' defined | ||
@@ -136,0 +157,0 @@ Validator.prototype.validateArray = function (instance, schema, options) { |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"dependencies": { | ||
@@ -6,0 +6,0 @@ "mocha": "~1.3", |
@@ -26,3 +26,3 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.png)](http://travis-ci.org/tdegrunt/jsonschema) | ||
| patternProperties | ✔ | | | ||
| additionalProperties | ✔ | | | ||
| additionalProperties | ✔ | ✔ | | ||
| items | ✔ | ✔ | | ||
@@ -29,0 +29,0 @@ | additionalItems | ✔ | | |
@@ -125,2 +125,60 @@ 'use strict'; | ||
}); | ||
}); | ||
describe('additionalProperties', function () { | ||
it('should validate if there are no additionalProperties', function () { | ||
this.validator.validate( | ||
{'name': 'test', 'nested': 'test2'}, | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'name': {'type': 'string'}, | ||
'nested': {'type': 'string'} | ||
}, | ||
'additionalProperties': false | ||
} | ||
).should.be.empty; | ||
}); | ||
it('should not validate if there are additionalProperties', function () { | ||
this.validator.validate( | ||
{'name': 'test', 'nested': 'test2', 'extraProp': 1}, | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'name': {'type': 'string'}, | ||
'nested': {'type': 'string'} | ||
}, | ||
'additionalProperties': false | ||
} | ||
).should.not.be.empty; | ||
}); | ||
it('should validate if the additionalProperties are compliant with additionalProperties', function () { | ||
this.validator.validate( | ||
{'name': 'test', 'nested': 'test2', 'extraProp': 1}, | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'name': {'type': 'string'}, | ||
'nested': {'type': 'string'} | ||
}, | ||
'additionalProperties': {'type': 'number'} | ||
} | ||
).should.be.empty; | ||
}); | ||
it('should not validate if the additionalProperties are not compliant with additionalProperties', function () { | ||
this.validator.validate( | ||
{'name': 'test', 'nested': 'test2', 'extraProp': '1'}, | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'name': {'type': 'string'}, | ||
'nested': {'type': 'string'} | ||
}, | ||
'additionalProperties': {'type': 'number'} | ||
} | ||
).should.not.be.empty; | ||
}); | ||
}); | ||
}); |
54182
1305