diff util/jsonschema.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
parents 88e1b94105ae
children ec54fe0003d5
line wrap: on
line diff
--- a/util/jsonschema.lua	Sun Jun 19 19:49:32 2022 +0200
+++ b/util/jsonschema.lua	Fri Jul 08 14:38:23 2022 +0200
@@ -1,3 +1,5 @@
+-- This file is generated from teal-src/util/jsonschema.lua
+
 local m_type = math.type or function (n)
 	return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
 end;
@@ -10,12 +12,12 @@
 
 local schema_t = {}
 
-local json_schema_object = {xml_t = {}}
-
-local type_validators = {}
+local json_schema_object = { xml_t = {} }
 
 local function simple_validate(schema, data)
-	if schema == "object" and type(data) == "table" then
+	if schema == nil then
+		return true
+	elseif schema == "object" and type(data) == "table" then
 		return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "string")
 	elseif schema == "array" and type(data) == "table" then
 		return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "number")
@@ -23,12 +25,82 @@
 		return m_type(data) == schema
 	elseif schema == "null" then
 		return data == null
+	elseif type(schema) == "table" then
+		for _, one in ipairs(schema) do
+			if simple_validate(one, data) then
+				return true
+			end
+		end
+		return false
 	else
 		return type(data) == schema
 	end
 end
 
-type_validators.string = function(schema, data)
+local complex_validate
+
+local function validate(schema, data, root)
+	if type(schema) == "boolean" then
+		return schema
+	else
+		return complex_validate(schema, data, root)
+	end
+end
+
+function complex_validate(schema, data, root)
+
+	if root == nil then
+		root = schema
+	end
+
+	if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then
+		local referenced = pointer.resolve(root, schema["$ref"]:sub(2))
+		if referenced ~= nil and referenced ~= root and referenced ~= schema then
+			if not validate(referenced, data, root) then
+				return false
+			end
+		end
+	end
+
+	if not simple_validate(schema.type, data) then
+		return false
+	end
+
+	if schema.type == "object" then
+		if type(data) == "table" then
+
+			for k in pairs(data) do
+				if not (type(k) == "string") then
+					return false
+				end
+			end
+		end
+	end
+
+	if schema.type == "array" then
+		if type(data) == "table" then
+
+			for i in pairs(data) do
+				if not (math.type(i) == "integer") then
+					return false
+				end
+			end
+		end
+	end
+
+	if schema["enum"] ~= nil then
+		local match = false
+		for _, v in ipairs(schema["enum"]) do
+			if v == data then
+
+				match = true
+				break
+			end
+		end
+		if not match then
+			return false
+		end
+	end
 
 	if type(data) == "string" then
 		if schema.maxLength and #data > schema.maxLength then
@@ -37,129 +109,85 @@
 		if schema.minLength and #data < schema.minLength then
 			return false
 		end
-		return true
 	end
-	return false
-end
+
+	if type(data) == "number" then
+		if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then
+			return false
+		end
+
+		if schema.maximum and not (data <= schema.maximum) then
+			return false
+		end
+
+		if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then
+			return false
+		end
 
-type_validators.number = function(schema, data)
-	if schema.multipleOf and data % schema.multipleOf ~= 0 then
-		return false
+		if schema.minimum and not (data >= schema.minimum) then
+			return false
+		end
+
+		if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then
+			return false
+		end
+	end
+
+	if schema.allOf then
+		for _, sub in ipairs(schema.allOf) do
+			if not validate(sub, data, root) then
+				return false
+			end
+		end
 	end
 
-	if schema.maximum and not (data <= schema.maximum) then
-		return false
+	if schema.oneOf then
+		local valid = 0
+		for _, sub in ipairs(schema.oneOf) do
+			if validate(sub, data, root) then
+				valid = valid + 1
+			end
+		end
+		if valid ~= 1 then
+			return false
+		end
 	end
 
-	if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then
-		return false
+	if schema.anyOf then
+		local match = false
+		for _, sub in ipairs(schema.anyOf) do
+			if validate(sub, data, root) then
+				match = true
+				break
+			end
+		end
+		if not match then
+			return false
+		end
 	end
 
-	if schema.minimum and not (data >= schema.minimum) then
-		return false
+	if schema["not"] then
+		if validate(schema["not"], data, root) then
+			return false
+		end
 	end
 
-	if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then
+	if schema["if"] ~= nil then
+		if validate(schema["if"], data, root) then
+			if schema["then"] then
+				return validate(schema["then"], data, root)
+			end
+		else
+			if schema["else"] then
+				return validate(schema["else"], data, root)
+			end
+		end
+	end
+
+	if schema.const ~= nil and schema.const ~= data then
 		return false
 	end
 
-	return true
-end
-
-type_validators.integer = type_validators.number
-
-local function validate(schema, data, root)
-	if type(schema) == "boolean" then
-		return schema
-	end
-	if type(schema) == "string" then
-		return simple_validate(schema, data)
-	end
-	if type(schema) == "table" then
-		if root == nil then
-			root = schema
-		end
-		if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then
-			local referenced = pointer.resolve(root, schema["$ref"]:sub(2))
-			if referenced ~= nil then
-				return validate(referenced, data, root)
-			end
-		end
-
-		if schema.allOf then
-			for _, sub in ipairs(schema.allOf) do
-				if not validate(sub, data, root) then
-					return false
-				end
-			end
-			return true
-		end
-
-		if schema.oneOf then
-			local valid = 0
-			for _, sub in ipairs(schema.oneOf) do
-				if validate(sub, data, root) then
-					valid = valid + 1
-				end
-			end
-			return valid == 1
-		end
-
-		if schema.anyOf then
-			for _, sub in ipairs(schema.anyOf) do
-				if validate(sub, data, root) then
-					return true
-				end
-			end
-			return false
-		end
-
-		if schema["not"] then
-			if validate(schema["not"], data, root) then
-				return false
-			end
-		end
-
-		if schema["if"] then
-			if validate(schema["if"], data, root) then
-				if schema["then"] then
-					return validate(schema["then"], data, root)
-				end
-			else
-				if schema["else"] then
-					return validate(schema["else"], data, root)
-				end
-			end
-		end
-
-		if schema.const ~= nil and schema.const ~= data then
-			return false
-		end
-
-		if schema["enum"] ~= nil then
-			for _, v in ipairs(schema["enum"]) do
-				if v == data then
-					return true
-				end
-			end
-			return false
-		end
-
-		if schema.type then
-			if not simple_validate(schema.type, data) then
-				return false
-			end
-
-			local validator = type_validators[schema.type]
-			if validator then
-				return validator(schema, data, root)
-			end
-		end
-		return true
-	end
-end
-
-type_validators.table = function(schema, data, root)
 	if type(data) == "table" then
 
 		if schema.maxItems and #data > schema.maxItems then
@@ -178,24 +206,28 @@
 			end
 		end
 
-		if schema.properties then
-			local additional = schema.additionalProperties or true
-			for k, v in pairs(data) do
-				if schema.propertyNames and not validate(schema.propertyNames, k, root) then
-					return false
-				end
-				local s = schema.properties[k] or additional
-				if not validate(s, v, root) then
+		if schema.propertyNames ~= nil then
+			for k in pairs(data) do
+				if not validate(schema.propertyNames, k, root) then
 					return false
 				end
 			end
-		elseif schema.additionalProperties then
-			for k, v in pairs(data) do
-				if schema.propertyNames and not validate(schema.propertyNames, k, root) then
+		end
+
+		if schema.properties then
+			for k, sub in pairs(schema.properties) do
+				if data[k] ~= nil and not validate(sub, data[k], root) then
 					return false
 				end
-				if not validate(schema.additionalProperties, v, root) then
-					return false
+			end
+		end
+
+		if schema.additionalProperties ~= nil then
+			for k, v in pairs(data) do
+				if schema.properties == nil or schema.properties[k] == nil then
+					if not validate(schema.additionalProperties, v, root) then
+						return false
+					end
 				end
 			end
 		end
@@ -212,9 +244,11 @@
 		end
 
 		local p = 0
-		if schema.prefixItems then
+		if schema.prefixItems ~= nil then
 			for i, s in ipairs(schema.prefixItems) do
-				if validate(s, data[i], root) then
+				if data[i] == nil then
+					break
+				elseif validate(s, data[i], root) then
 					p = i
 				else
 					return false
@@ -222,7 +256,7 @@
 			end
 		end
 
-		if schema.items then
+		if schema.items ~= nil then
 			for i = p + 1, #data do
 				if not validate(schema.items, data[i], root) then
 					return false
@@ -230,7 +264,7 @@
 			end
 		end
 
-		if schema.contains then
+		if schema.contains ~= nil then
 			local found = false
 			for i = 1, #data do
 				if validate(schema.contains, data[i], root) then
@@ -242,37 +276,9 @@
 				return false
 			end
 		end
-
-		return true
 	end
-	return false
-end
-
-type_validators.object = function(schema, data, root)
-	if type(data) == "table" then
-		for k in pairs(data) do
-			if not (type(k) == "string") then
-				return false
-			end
-		end
 
-		return type_validators.table(schema, data, root)
-	end
-	return false
-end
-
-type_validators.array = function(schema, data, root)
-	if type(data) == "table" then
-
-		for i in pairs(data) do
-			if not (type(i) == "number") then
-				return false
-			end
-		end
-
-		return type_validators.table(schema, data, root)
-	end
-	return false
+	return true
 end
 
 json_schema_object.validate = validate;