From 334a5c427b67ef995370e0f77b3bdeefb5f3eb92 Mon Sep 17 00:00:00 2001 From: Axel Haustant Date: Sat, 27 Dec 2014 05:08:15 +0100 Subject: [PATCH] Handle undefined properties --- source/object.js | 3 +++ test/tests/05 - Objects/07 - undefined.js | 24 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/tests/05 - Objects/07 - undefined.js diff --git a/source/object.js b/source/object.js index f5146d2..05d2e8f 100644 --- a/source/object.js +++ b/source/object.js @@ -51,6 +51,9 @@ ValidatorContext.prototype.validateObjectProperties = function validateObjectPro for (var key in data) { var keyPointerPath = dataPointerPath + "/" + key.replace(/~/g, '~0').replace(/\//g, '~1'); var foundMatch = false; + if (data[key] === undefined && (!schema.required || schema.required.indexOf(key) < 0)) { + continue; + } if (schema.properties !== undefined && schema.properties[key] !== undefined) { foundMatch = true; if (error = this.validateAll(data[key], schema.properties[key], [key], ["properties", key], keyPointerPath)) { diff --git a/test/tests/05 - Objects/07 - undefined.js b/test/tests/05 - Objects/07 - undefined.js new file mode 100644 index 0000000..f5a7119 --- /dev/null +++ b/test/tests/05 - Objects/07 - undefined.js @@ -0,0 +1,24 @@ +describe("Objects 07", function() { + it("validate an optional undefined property", function () { + var data = {optional: undefined}; + var schema = { + properties: { + optional: {"type": "string"}, + } + }; + var valid = tv4.validate(data, schema); + assert.isTrue(valid); + }); + + it("does not validate a undefined required property", function () { + var data = {required: undefined}; + var schema = { + properties: { + required: {"type": "string"} + }, + required: ['required'] + }; + var valid = tv4.validate(data, schema); + assert.isFalse(valid); + }); +});