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

Handle undefined properties #175

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions source/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
24 changes: 24 additions & 0 deletions test/tests/05 - Objects/07 - undefined.js
Original file line number Diff line number Diff line change
@@ -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);
});
});