Comparison

spec/util_jsonschema_spec.lua @ 12579:ca6a43fe0231 0.12

util.jsonschema: Fix validation to not assume presence of "type" field MattJ reported a curious issue where validation did not work as expected. Primarily that the "type" field was expected to be mandatory, and thus leaving it out would result in no checks being performed. This was likely caused by misreading during initial development. Spent some time testing against https://github.com/json-schema-org/JSON-Schema-Test-Suite.git and discovered a multitude of issues, far too many to bother splitting into separate commits. More than half of them fail. Many because of features not implemented, which have been marked NYI. For example, some require deep comparisons e.g. when objects or arrays are present in enums fields. Some because of quirks with how Lua differs from JavaScript, e.g. no distinct array or object types. Tests involving fractional floating point numbers. We're definitely not going to follow references to remote resources. Or deal with UTF-16 sillyness. One test asserted that 1.0 is an integer, where Lua 5.3+ will disagree.
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jul 2022 14:38:23 +0200
child 12756:cd7da871ce10
comparison
equal deleted inserted replaced
12557:ee5b061588ea 12579:ca6a43fe0231
1 local js = require "util.jsonschema";
2 local json = require "util.json";
3 local lfs = require "lfs";
4
5 -- https://github.com/json-schema-org/JSON-Schema-Test-Suite.git 2.0.0-550-g88d6948
6 local test_suite_dir = "spec/JSON-Schema-Test-Suite/tests/draft2020-12"
7 if lfs.attributes(test_suite_dir, "mode") ~= "directory" then return end
8
9 -- Tests to skip and short reason why (NYI = not yet implemented)
10 local skip = {
11 ["ref.json:0:3"] = "NYI additionalProperties";
12 ["ref.json:3:2"] = "FIXME investigate, util.jsonpath issue?",
13 ["ref.json:6:1"] = "NYI",
14 ["required.json:0:2"] = "distinguishing objects from arrays",
15 ["additionalProperties.json:0:2"] = "distinguishing objects from arrays",
16 ["additionalProperties.json:0:5"] = "NYI",
17 ["additionalProperties.json:1:0"] = "NYI",
18 ["anchor.json"] = "$anchor NYI",
19 ["const.json:1"] = "deepcompare",
20 ["const.json:13:2"] = "IEEE 754 equality",
21 ["const.json:2"] = "deepcompare",
22 ["const.json:8"] = "deepcompare",
23 ["const.json:9"] = "deepcompare",
24 ["contains.json:0:5"] = "distinguishing objects from arrays",
25 ["defs.json"] = "need built-in meta-schema",
26 ["dependentRequired.json"] = "NYI",
27 ["dependentSchemas.json"] = "NYI",
28 ["dynamicRef.json"] = "NYI",
29 ["enum.json:1:3"] = "deepcompare",
30 ["id.json"] = "NYI",
31 ["maxContains.json"] = "NYI",
32 ["maxLength.json:0:4"] = "UTF-16",
33 ["maxProperties.json"] = "NYI",
34 ["minContains.json"] = "NYI",
35 ["minLength.json:0:4"] = "UTF-16",
36 ["minProperties.json"] = "NYI",
37 ["multipleOf.json:1"] = "multiples of IEEE 754 fractions",
38 ["multipleOf.json:2"] = "multiples of IEEE 754 fractions",
39 ["pattern.json"] = "NYI",
40 ["patternProperties.json"] = "NYI",
41 ["properties.json:1:2"] = "NYI",
42 ["properties.json:1:3"] = "NYI",
43 ["ref.json:14"] = "NYI",
44 ["ref.json:15"] = "NYI",
45 ["ref.json:16"] = "NYI",
46 ["ref.json:17"] = "NYI",
47 ["ref.json:18"] = "NYI",
48 ["ref.json:13"] = "NYI",
49 ["ref.json:19"] = "NYI",
50 ["ref.json:11"] = "NYI",
51 ["ref.json:12:1"] = "FIXME",
52 ["refRemote.json"] = "DEFINITELY NYI",
53 ["type.json:3:4"] = "distinguishing objects from arrays",
54 ["type.json:3:6"] = "null is weird",
55 ["type.json:4:3"] = "distinguishing objects from arrays",
56 ["type.json:4:6"] = "null is weird",
57 ["type.json:9:4"] = "null is weird",
58 ["type.json:9:6"] = "null is weird",
59 ["unevaluatedItems.json"] = "NYI",
60 ["unevaluatedProperties.json"] = "NYI",
61 ["uniqueItems.json:0:11"] = "deepcompare",
62 ["uniqueItems.json:0:13"] = "deepcompare",
63 ["uniqueItems.json:0:14"] = "deepcompare",
64 ["uniqueItems.json:0:22"] = "deepcompare",
65 ["uniqueItems.json:0:24"] = "deepcompare",
66 ["uniqueItems.json:0:9"] = "deepcompare",
67 ["unknownKeyword.json"] = "NYI",
68 ["vocabulary.json"] = "NYI",
69 };
70
71 local function label(s, i)
72 return string.format("%s:%d", s, i-1);
73 end
74
75 describe("util.jsonschema.validate", function()
76 for test_case_file in lfs.dir(test_suite_dir) do
77 -- print(skip[test_case_file] and "do " or "skip", test_case_file)
78 if test_case_file:sub(-5) == ".json" and not skip[test_case_file] then
79 describe(test_case_file, function()
80 local test_cases;
81 setup(function()
82 local f = assert(io.open(test_suite_dir .. "/" .. test_case_file));
83 local rawdata = assert(f:read("*a"), "failed to read " .. test_case_file)
84 test_cases = assert(json.decode(rawdata), "failed to parse " .. test_case_file)
85 end)
86 describe("tests", function()
87 for i, schema_test in ipairs(test_cases) do
88 local generic_label = label(test_case_file, i);
89 describe(schema_test.description or generic_label, function()
90 for j, test in ipairs(schema_test.tests) do
91 local specific_label = label(generic_label, j);
92 ((skip[generic_label] or skip[specific_label]) and pending or it)(test.description, function()
93 assert.equal(test.valid, js.validate(schema_test.schema, test.data), specific_label .. " " .. test.description);
94 end)
95 end
96 end)
97 end
98 end)
99 end)
100 end
101 end
102 end);