From 935ebcfb094ae2b49c469fdc5e8b23b6f4d87098 Mon Sep 17 00:00:00 2001 From: Roel Kluin Date: Mon, 9 Feb 2015 18:32:59 +0100 Subject: [PATCH 1/3] If the last stack_length is 0 we need to copy each key or we overwrite the reference to the original object. Signed-off-by: Roel Kluin --- lib/jjv.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/jjv.js b/lib/jjv.js index 83e95c4..b79938a 100644 --- a/lib/jjv.js +++ b/lib/jjv.js @@ -61,7 +61,21 @@ var copy_stack = function (new_stack, old_stack) { var stack_last = new_stack.length-1, key = new_stack[stack_last].key; - old_stack[stack_last].object[key] = new_stack[stack_last].object[key]; + + if (stack_last !== 0) { + old_stack[stack_last].object[key] = new_stack[stack_last].object[key]; + } else { + + Object.keys(new_stack[stack_last].object[key]).forEach(function(k) { + old_stack[stack_last].object[key][k] = new_stack[stack_last].object[key][k]; + }); + + Object.keys(old_stack[stack_last].object[key]).forEach(function(k) { + if (!new_stack[stack_last].object[key].hasOwnProperty(k)) { + delete old_stack[stack_last].object[key][k]; + } + }); + } }; var handled = { From d2272bf2259e08dc92e4f6449683ae9ce41fab4b Mon Sep 17 00:00:00 2001 From: Roel Kluin Date: Tue, 28 Apr 2015 16:46:08 +0200 Subject: [PATCH 2/3] Enable providing validate-options and result validation in tests Signed-off-by: Roel Kluin --- test/test-suite.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/test-suite.js b/test/test-suite.js index 3307f10..4daca5c 100644 --- a/test/test-suite.js +++ b/test/test-suite.js @@ -17,15 +17,20 @@ env.addSchema('http://localhost:1234/subSchemas.json', { "integer": { "type": "integer" }, "refToInteger": { "$ref": "#/integer" } }); - + function runTest(i, j, k) { var schema = tests[i][j].schema; var test = tests[i][j].tests[k]; + var validateOptions = test.validateOptions || env.defaultOptions; + var res = env.validate(schema, test.data, validateOptions); it(test.description, function () { - if (test.valid) - expect(env.validate(schema, test.data)).to.be.equal(null); - else - expect(env.validate(schema, test.data)).not.to.be.equal(null); + if (test.valid) { + expect(res).to.be.equal(null); + if (test.hasOwnProperty('result')) + expect(test.data).to.eql(test.result); + } else { + expect(res).not.to.be.equal(null); + } }); } From 74e022c740b727ea46879e4106e31bf98da83caa Mon Sep 17 00:00:00 2001 From: Roel Kluin Date: Tue, 28 Apr 2015 17:03:14 +0200 Subject: [PATCH 3/3] add oneOf test to show failure Signed-off-by: Roel Kluin --- test/fixtures/oneOf.json | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/fixtures/oneOf.json b/test/fixtures/oneOf.json index 1eaa4e4..18ae710 100644 --- a/test/fixtures/oneOf.json +++ b/test/fixtures/oneOf.json @@ -64,5 +64,81 @@ "valid": false } ] + }, + { + "description": "oneOf, strip other", + "schema": { + "definition": { + "one": { + "title": "schema 1", + "properties": { + "tool" : {"enum": ["set"] }, + "field": { "type": "string", "default": "field1"}, + "s1" : { "type": "string", "default": "on1"} + }, + "required": ["tool"] + }, + "two": { + "title": "schema 2", + "properties": { + "tool" : {"enum": ["set 2"] }, + "s2" : { "type": "string", "default": "on2"} + }, + "required": ["tool"] + } + }, + "oneOf": [{"$ref": "#/definition/one"}, {"$ref": "#/definition/two"}] + }, + "tests": [ + { + "description": "oneOf valid: get tool 'set', defaults 'field1' & 'on1', strip other", + "data": { + "tool": "set", + "other": "remove" + }, + "valid": true, + "result": { + "tool": "set", + "field": "field1", + "s1": "on1" + }, + "validateOptions": {"useDefault": true, "removeAdditional": true, "checkRequired": true} + }, { + "description": "oneOf valid, get set, field met and strip other", + "data": { + "tool": "set", + "field": "met", + "other": "remove" + }, + "valid": true, + "result": { + "tool": "set", + "field": "met", + "s1": "on1" + }, + "validateOptions": {"useDefault": true, "removeAdditional": true, "checkRequired": true} + }, { + "description": "oneOf valid, get set 2 and strip other", + "data": { + "tool": "set 2", + "other": "remove" + }, + "valid": true, + "result": { + "tool": "set 2", + "s2": "on2" + }, + "validateOptions": {"useDefault": true, "removeAdditional": true, "checkRequired": true} + }, { + "description": "oneOf invalid due to tool", + "data": { + "tool": "unknown", + "other": "remove" + }, + "valid": false, + "validateOptions": {"useDefault": true, "removeAdditional": true, "checkRequired": true} + } + ] } + ]