diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/modules/tests/xpcshell/test_JsonSchema.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/tests/xpcshell/test_JsonSchema.js')
-rw-r--r-- | toolkit/modules/tests/xpcshell/test_JsonSchema.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/toolkit/modules/tests/xpcshell/test_JsonSchema.js b/toolkit/modules/tests/xpcshell/test_JsonSchema.js new file mode 100644 index 0000000000..b3a09049ac --- /dev/null +++ b/toolkit/modules/tests/xpcshell/test_JsonSchema.js @@ -0,0 +1,88 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +const { JsonSchema } = ChromeUtils.importESModule( + "resource://gre/modules/JsonSchema.sys.mjs" +); + +add_task(function test_basicSchema() { + info("Testing validation of a basic schema"); + const schema = { + type: "object", + properties: { + id: { type: "number" }, + }, + required: ["id"], + }; + + const validator = new JsonSchema.Validator(schema); + + Assert.deepEqual( + JsonSchema.validate({ id: 123 }, schema), + { valid: true, errors: [] }, + "Validation of basic schemas with validate()" + ); + + Assert.deepEqual( + validator.validate({ id: 123 }, schema), + { valid: true, errors: [] }, + "Validation of basic schemas with Validator" + ); + + Assert.ok( + !JsonSchema.validate({}, schema).valid, + "Validation of basic schemas with validate()" + ); + Assert.ok( + !validator.validate({}).valid, + "Validation of basic schemas with Validator" + ); +}); + +add_task(function test_mozUrlFormat() { + info("Testing custom string format 'moz-url-format'"); + const schema = { + type: "string", + format: "moz-url-format", + }; + + { + const obj = "https://www.mozilla.org/%LOCALE%/"; + Assert.deepEqual( + JsonSchema.validate(obj, schema), + { valid: true, errors: [] }, + "Substitution of a valid variable validates" + ); + } + + { + const obj = "https://mozilla.org/%BOGUS%/"; + + Assert.equal( + Services.urlFormatter.formatURL(obj), + obj, + "BOGUS is an invalid variable for the URL formatter service" + ); + + Assert.ok( + !JsonSchema.validate(obj, { type: "string", format: "uri" }).valid, + "A moz-url-format string does not validate as a URI" + ); + + Assert.deepEqual( + JsonSchema.validate(obj, schema), + { + valid: false, + errors: [ + { + instanceLocation: "#", + keyword: "format", + keywordLocation: "#/format", + error: `String does not match format "moz-url-format".`, + }, + ], + }, + "Substitution of an invalid variable does not validate" + ); + } +}); |