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

Skipped format validation for allowed null values. #241

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
1 change: 1 addition & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scripts": [
"tv4.js",
"lang/de.js",
"lang/es.js",
"lang/fr.js",
"lang/nb.js",
"lang/pl-PL.js",
Expand Down
12 changes: 12 additions & 0 deletions source/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ ValidatorContext.prototype.validateBasic = function validateBasic(data, schema,
if (error = this.validateType(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type");
}

var dataType = typeof data;
if (data === null) {
dataType = "null";
} else if (Array.isArray(data)) {
dataType = "array";
}

if (dataType === "null" || dataType === "undefined") {
return null;
}

if (error = this.validateEnum(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type");
}
Expand Down
2 changes: 1 addition & 1 deletion source/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ValidatorContext.prototype.validateObjectRequiredProperties = function validateO
if (schema.required !== undefined) {
for (var i = 0; i < schema.required.length; i++) {
var key = schema.required[i];
if (data[key] === undefined) {
if (data[key] === undefined || data[key] === null) {
var error = this.createError(ErrorCodes.OBJECT_REQUIRED, {key: key}, '', '/required/' + i, null, data, schema);
if (this.handleError(error)) {
return error;
Expand Down
9 changes: 9 additions & 0 deletions source/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ ValidatorContext.prototype.validateAll = function (data, schema, dataPathParts,
return this.handleError(error);
};
ValidatorContext.prototype.validateFormat = function (data, schema) {
var allowedTypes = schema.type;
if (!Array.isArray(allowedTypes)) {
allowedTypes = [allowedTypes];
}

if (data === null && allowedTypes.indexOf('null') !== -1) {
return null;
}

if (typeof schema.format !== 'string' || !this.formatValidators[schema.format]) {
return null;
}
Expand Down
50 changes: 47 additions & 3 deletions test/all_concat.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/all_concat.js.map

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions test/tests/11 - format/01 - register validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,43 @@ describe("Registering custom validator", function () {
assert.isFalse(result2.valid);
assert.includes(result2.error.message, 'break 2');
});

it("Custom validator should be skipped for null values", function () {
tv4.addFormat('null-format', function (data) {
if (data !== "test") {
return "string does not match";
}
});

var schema = {type: ["string", "null"], format: 'null-format'};
var data1 = "test";
var data2 = null;

assert.isTrue(tv4.validate(data1, schema));
assert.isTrue(tv4.validate(data2, schema));
});

it("Validator should be skipped for null (undefined) enum values", function () {
var schema = {
"type": "object",
"properties": {
"a": {
type: "string"
},
"b": {
"type": ["string", "null"],
"enum": ['one', 'two', 'three']
}
}
};
var data1 = {a: "something", b: "test"};
var data2 = {a: "something", b: "one"};
var data3 = {a: "something", b: null};
var data4 = {a: "something"};

assert.isFalse(tv4.validate(data1, schema));
assert.isTrue(tv4.validate(data2, schema));
assert.isTrue(tv4.validate(data3, schema));
assert.isTrue(tv4.validate(data4, schema));
});
});
11 changes: 8 additions & 3 deletions test/tests/Misc/enum null failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ describe("Enum object/null failure", function () {
"type": "object",
"required": ["value"],
"properties": {
"key": {
"type": "string"
},
"value": {
"enum": [6, "foo", [], true, {"foo": 12}]
}
}
};

var data = {value: null}; // Somehow this is only a problem when a *property* is null, not the root

var data = {key: "test", value: null}; // Somehow this is only a problem when a *property* is null, not the root
var result = tv4.validateMultiple(data, schema);

assert.isFalse(result.valid, 'validateMultiple() should return invalid');

data = {key: "test"}; // Undefined required property.
result = tv4.validateMultiple(data, schema);
assert.isFalse(result.valid, 'validateMultiple() should return invalid');
});
});
26 changes: 24 additions & 2 deletions tv4.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tv4.min.js

Large diffs are not rendered by default.