Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix schema validation with OneOf or AnyOf #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/jjv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
76 changes: 76 additions & 0 deletions test/fixtures/oneOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
]
}

]
15 changes: 10 additions & 5 deletions test/test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand Down