From e700edc50f3bd7f05d45bb4410396178811f3561 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 8 Jul 2022 14:38:23 +0200 Subject: util.jsonschema: Fix validation to not assume presence of "type" field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- spec/util_jsonschema_spec.lua | 102 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 spec/util_jsonschema_spec.lua (limited to 'spec') diff --git a/spec/util_jsonschema_spec.lua b/spec/util_jsonschema_spec.lua new file mode 100644 index 00000000..74da2c07 --- /dev/null +++ b/spec/util_jsonschema_spec.lua @@ -0,0 +1,102 @@ +local js = require "util.jsonschema"; +local json = require "util.json"; +local lfs = require "lfs"; + +-- https://github.com/json-schema-org/JSON-Schema-Test-Suite.git 2.0.0-550-g88d6948 +local test_suite_dir = "spec/JSON-Schema-Test-Suite/tests/draft2020-12" +if lfs.attributes(test_suite_dir, "mode") ~= "directory" then return end + +-- Tests to skip and short reason why (NYI = not yet implemented) +local skip = { + ["ref.json:0:3"] = "NYI additionalProperties"; + ["ref.json:3:2"] = "FIXME investigate, util.jsonpath issue?", + ["ref.json:6:1"] = "NYI", + ["required.json:0:2"] = "distinguishing objects from arrays", + ["additionalProperties.json:0:2"] = "distinguishing objects from arrays", + ["additionalProperties.json:0:5"] = "NYI", + ["additionalProperties.json:1:0"] = "NYI", + ["anchor.json"] = "$anchor NYI", + ["const.json:1"] = "deepcompare", + ["const.json:13:2"] = "IEEE 754 equality", + ["const.json:2"] = "deepcompare", + ["const.json:8"] = "deepcompare", + ["const.json:9"] = "deepcompare", + ["contains.json:0:5"] = "distinguishing objects from arrays", + ["defs.json"] = "need built-in meta-schema", + ["dependentRequired.json"] = "NYI", + ["dependentSchemas.json"] = "NYI", + ["dynamicRef.json"] = "NYI", + ["enum.json:1:3"] = "deepcompare", + ["id.json"] = "NYI", + ["maxContains.json"] = "NYI", + ["maxLength.json:0:4"] = "UTF-16", + ["maxProperties.json"] = "NYI", + ["minContains.json"] = "NYI", + ["minLength.json:0:4"] = "UTF-16", + ["minProperties.json"] = "NYI", + ["multipleOf.json:1"] = "multiples of IEEE 754 fractions", + ["multipleOf.json:2"] = "multiples of IEEE 754 fractions", + ["pattern.json"] = "NYI", + ["patternProperties.json"] = "NYI", + ["properties.json:1:2"] = "NYI", + ["properties.json:1:3"] = "NYI", + ["ref.json:14"] = "NYI", + ["ref.json:15"] = "NYI", + ["ref.json:16"] = "NYI", + ["ref.json:17"] = "NYI", + ["ref.json:18"] = "NYI", + ["ref.json:13"] = "NYI", + ["ref.json:19"] = "NYI", + ["ref.json:11"] = "NYI", + ["ref.json:12:1"] = "FIXME", + ["refRemote.json"] = "DEFINITELY NYI", + ["type.json:3:4"] = "distinguishing objects from arrays", + ["type.json:3:6"] = "null is weird", + ["type.json:4:3"] = "distinguishing objects from arrays", + ["type.json:4:6"] = "null is weird", + ["type.json:9:4"] = "null is weird", + ["type.json:9:6"] = "null is weird", + ["unevaluatedItems.json"] = "NYI", + ["unevaluatedProperties.json"] = "NYI", + ["uniqueItems.json:0:11"] = "deepcompare", + ["uniqueItems.json:0:13"] = "deepcompare", + ["uniqueItems.json:0:14"] = "deepcompare", + ["uniqueItems.json:0:22"] = "deepcompare", + ["uniqueItems.json:0:24"] = "deepcompare", + ["uniqueItems.json:0:9"] = "deepcompare", + ["unknownKeyword.json"] = "NYI", + ["vocabulary.json"] = "NYI", +}; + +local function label(s, i) + return string.format("%s:%d", s, i-1); +end + +describe("util.jsonschema.validate", function() + for test_case_file in lfs.dir(test_suite_dir) do + -- print(skip[test_case_file] and "do " or "skip", test_case_file) + if test_case_file:sub(-5) == ".json" and not skip[test_case_file] then + describe(test_case_file, function() + local test_cases; + setup(function() + local f = assert(io.open(test_suite_dir .. "/" .. test_case_file)); + local rawdata = assert(f:read("*a"), "failed to read " .. test_case_file) + test_cases = assert(json.decode(rawdata), "failed to parse " .. test_case_file) + end) + describe("tests", function() + for i, schema_test in ipairs(test_cases) do + local generic_label = label(test_case_file, i); + describe(schema_test.description or generic_label, function() + for j, test in ipairs(schema_test.tests) do + local specific_label = label(generic_label, j); + ((skip[generic_label] or skip[specific_label]) and pending or it)(test.description, function() + assert.equal(test.valid, js.validate(schema_test.schema, test.data), specific_label .. " " .. test.description); + end) + end + end) + end + end) + end) + end + end +end); -- cgit v1.2.3