Bug 1530465 - Add support for JSON type in schema validator. r=Felipe

Differential Revision: https://phabricator.services.mozilla.com/D21048

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Michael Kaply 2019-02-26 15:12:44 +00:00
Родитель 52bdd9d78e
Коммит 4e893ec222
2 изменённых файлов: 57 добавлений и 0 удалений

Просмотреть файл

@ -156,6 +156,22 @@ function validateAndParseParamRecursive(param, properties) {
}
return [true, parsedObj];
}
case "JSON":
if (typeof(param) == "object") {
return [true, param];
}
try {
let json = JSON.parse(param);
if (typeof(json) != "object") {
log.error("JSON was not an object");
return [false, null];
}
return [true, json];
} catch (e) {
log.error("JSON string couldn't be parsed");
return [false, null];
}
}
return [false, null];

Просмотреть файл

@ -447,3 +447,44 @@ add_task(async function test_patternProperties() {
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters({}, schema);
}, /Invalid property pattern/, "Checking that invalid property patterns throw");
});
add_task(async function test_JSON_type() {
let schema = {
type: "JSON",
};
let valid, parsed;
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters({
"a": "b",
}, schema);
ok(valid, "Object is valid");
ok(typeof(parsed) == "object", "parsed in an object");
is(parsed.a, "b", "parsedProperty is correct");
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
"{\"a\": \"b\"}"
, schema);
ok(valid, "Object is valid");
ok(typeof(parsed) == "object", "parsed in an object");
is(parsed.a, "b", "parsedProperty is correct");
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
"{This{is{not{JSON}}}}"
, schema);
ok(!valid, "Object is not valid since JSON was incorrect");
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
"0"
, schema);
ok(!valid, "Object is not valid since input wasn't an object");
[valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
"true"
, schema);
ok(!valid, "Object is not valid since input wasn't an object");
});