Changeset

13385:72d7830505f0

util.jsonschema: Return basic structured validation response
author Kim Alvefur <zash@zash.se>
date Sat, 02 Dec 2023 21:45:38 +0100
parents 13384:57ad1dfd8e8b
children 13386:33e5edbd6a4a
files spec/util_jsonschema_spec.lua teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 3 files changed, 218 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_jsonschema_spec.lua	Sun Dec 03 15:46:33 2023 +0100
+++ b/spec/util_jsonschema_spec.lua	Sat Dec 02 21:45:38 2023 +0100
@@ -56,6 +56,7 @@
 	["not.json:6"] = "NYI",
 	["refRemote.json"] = "DEFINITELY NYI",
 	["required.json:0:2"] = "distinguishing objects from arrays",
+	["type.json:0:1"] = "1.0 is not an integer!",
 	["type.json:3:4"] = "distinguishing objects from arrays",
 	["type.json:3:6"] = "null is weird",
 	["type.json:4:3"] = "distinguishing objects from arrays",
--- a/teal-src/prosody/util/jsonschema.tl	Sun Dec 03 15:46:33 2023 +0100
+++ b/teal-src/prosody/util/jsonschema.tl	Sat Dec 02 21:45:38 2023 +0100
@@ -131,7 +131,7 @@
 	writeOnly : boolean
 
 	-- methods
-	validate : function ( schema_t, any, json_schema_object ) : boolean
+	validate : function (schema : schema_t, data : any, root : json_schema_object, sloc : string, iloc : string, errs:errors) : boolean, errors
 end
 
 -- TODO validator function per schema property
@@ -159,26 +159,41 @@
 	end
 end
 
-local function validate (schema : schema_t, data : any, root : json_schema_object) : boolean
+local record validation_error
+	instanceLocation : string
+	schemaLocation : string
+	error : string
+end
+local type errors = { validation_error }
+local function mkerr(sloc:string,iloc:string,err:string) : validation_error
+	return { schemaLocation = sloc; instanceLocation = iloc; error = err };
+end
+
+local function validate (schema : schema_t, data : any, root : json_schema_object, sloc : string, iloc : string, errs:errors) : boolean, errors
 	if schema is boolean then
 		return schema
 	end
 
 	if root == nil then
-		root = schema
+		root = schema as json_schema_object
+		iloc = ""
+		sloc = ""
+		errs = {};
 	end
 
 	if schema["$ref"] and schema["$ref"]:sub(1,1) == "#" then
 		local referenced = pointer.resolve(root as table, schema["$ref"]:sub(2)) as schema_t
 		if referenced ~= nil and referenced ~= root and referenced ~= schema then
-			if not validate(referenced, data, root) then
-				return false;
+			if not validate(referenced, data, root, schema["$ref"], iloc, errs) then
+				table.insert(errs, mkerr(sloc.."/$ref", iloc, "Subschema failed validation"))
+				return false, errs;
 			end
 		end
 	end
 
 	if not simple_validate(schema.type, data) then
-		return false;
+		table.insert(errs, mkerr(sloc.."/type", iloc, "unexpected type"));
+		return false, errs;
 	end
 
 	if schema.type == "object" then
@@ -186,7 +201,8 @@
 			-- just check that there the keys are all strings
 			for k in pairs(data) do
 				if not k is string then
-					return false
+					table.insert(errs, mkerr(sloc.."/type", iloc, "'object' had non-string keys"));
+					return false, errs;
 				end
 			end
 		end
@@ -197,7 +213,8 @@
 			-- just check that there the keys are all numbers
 			for i in pairs(data) do
 				if not i is integer then
-					return false
+					table.insert(errs, mkerr(sloc.."/type", iloc, "'array' had non-integer keys"));
+					return false, errs;
 				end
 			end
 		end
@@ -213,7 +230,8 @@
 			end
 		end
 		if not match then
-			return false
+			table.insert(errs, mkerr(sloc.."/enum", iloc, "not one of the enumerated values"));
+			return false, errs;
 		end
 	end
 
@@ -221,91 +239,110 @@
 	-- TODO use utf8.len?
 	if data is string then
 		if schema.maxLength and utf8_len(data) > schema.maxLength then
-			return false
+			table.insert(errs, mkerr(sloc.."/maxLength", iloc, "string too long"))
+			return false, errs;
 		end
 		if schema.minLength and utf8_len(data) < schema.minLength then
-			return false
+			table.insert(errs, mkerr(sloc.."/maxLength", iloc, "string too short"))
+			return false, errs;
 		end
 		if schema.luaPattern and not data:match(schema.luaPattern) then
-			return false
+			table.insert(errs, mkerr(sloc.."/luaPattern", iloc, "string does not match pattern"))
+			return false, errs;
 		end
 	end
 
 	if data is number then
 		if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then
-			return false
+			table.insert(errs, mkerr(sloc.."/luaPattern", iloc, "not a multiple"))
+			return false, errs;
 		end
 
 		if schema.maximum and not ( data <= schema.maximum ) then
-			return false
+			table.insert(errs, mkerr(sloc.."/maximum", iloc, "number exceeds maximum"))
+			return false, errs;
 		end
 
 		if schema.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) then
-			return false
+			table.insert(errs, mkerr(sloc.."/exclusiveMaximum", iloc, "number exceeds exclusive maximum"))
+			return false, errs;
 		end
 
 		if schema.minimum and not ( data >= schema.minimum ) then
-			return false
+			table.insert(errs, mkerr(sloc.."/minimum", iloc, "number below minimum"))
+			return false, errs;
 		end
 
 		if schema.exclusiveMinimum and not ( data > schema.exclusiveMinimum ) then
-			return false
+			table.insert(errs, mkerr(sloc.."/exclusiveMinimum", iloc, "number below exclusive minimum"))
+			return false, errs;
 		end
 	end
 
 	if schema.allOf then
-		for _, sub in ipairs(schema.allOf) do
-			if not validate(sub, data, root) then
-				return false
+		for i, sub in ipairs(schema.allOf) do
+			if not validate(sub, data, root, sloc.."/allOf/"..i, iloc, errs) then
+				table.insert(errs, mkerr(sloc.."/allOf", iloc, "did not match all subschemas"))
+				return false, errs;
 			end
 		end
 	end
 
 	if schema.oneOf then
 		local valid = 0
-		for _, sub in ipairs(schema.oneOf) do
-			if validate(sub, data, root) then
+		for i, sub in ipairs(schema.oneOf) do
+			if validate(sub, data, root, sloc.."/oneOf"..i, iloc, errs) then
 				valid = valid + 1
 			end
 		end
 		if valid ~= 1 then
-			return false
+			table.insert(errs, mkerr(sloc.."/oneOf", iloc, "did not match exactly one subschema"))
+			return false, errs;
 		end
 	end
 
 	if schema.anyOf then
 		local match = false
-		for _, sub in ipairs(schema.anyOf) do
-			if validate(sub, data, root) then
+		for i, sub in ipairs(schema.anyOf) do
+			if validate(sub, data, root, sloc.."/anyOf/"..i, iloc, errs) then
 				match = true
 				break
 			end
 		end
 		if not match then
-			return false
+			table.insert(errs, mkerr(sloc.."/anyOf", iloc, "did not match any subschema"))
+			return false, errs;
 		end
 	end
 
 	if schema["not"] then
-		if validate(schema["not"], data, root) then
-			return false
+		if validate(schema["not"], data, root, sloc.."/not", iloc, errs) then
+			table.insert(errs, mkerr(sloc.."/not", iloc, "did match subschema"))
+			return false, errs;
 		end
 	end
 
 	if schema["if"] ~= nil then
-		if validate(schema["if"], data, root) then
+		if validate(schema["if"], data, root, sloc.."/if", iloc, errs) then
 			if schema["then"] then
-				return validate(schema["then"], data, root)
+				if not validate(schema["then"], data, root, sloc.."/then", iloc, errs) then
+					table.insert(errs, mkerr(sloc.."/then", iloc, "did not match subschema"))
+					return false, errs;
+				end
 			end
 		else
 			if schema["else"] then
-				return validate(schema["else"], data, root)
+				if not validate(schema["else"], data, root, sloc.."/else", iloc, errs) then
+					table.insert(errs, mkerr(sloc.."/else", iloc, "did not match subschema"))
+					return false, errs;
+				end
 			end
 		end
 	end
 
 	if schema.const ~= nil and schema.const ~= data then
-		return false
+		table.insert(errs, mkerr(sloc.."/const", iloc, "did not match constant value"))
+		return false, errs;
 	end
 
 	if data is table then
@@ -314,17 +351,20 @@
 		-- tables
 
 		if schema.maxItems and #(data as {any}) > schema.maxItems then
-			return false
+			table.insert(errs, mkerr(sloc.."/maxItems", iloc, "too many items"))
+			return false, errs;
 		end
 
 		if schema.minItems and #(data as {any}) < schema.minItems then
-			return false
+			table.insert(errs, mkerr(sloc.."/minItems", iloc, "too few items"))
+			return false, errs;
 		end
 
 		if schema.required then
 			for _, k in ipairs(schema.required) do
 				if data[k] == nil then
-					return false
+					table.insert(errs, mkerr(sloc.."/required", iloc.."/"..tostring(k), "missing required property"))
+					return false, errs;
 				end
 			end
 		end
@@ -334,7 +374,8 @@
 				if data[k] ~= nil then
 					for _, req in ipairs(reqs) do
 						if data[req] == nil then
-							return false
+							table.insert(errs, mkerr(sloc.."/dependentRequired", iloc, "missing dependent required property"))
+							return false, errs;
 						end
 					end
 				end
@@ -344,8 +385,9 @@
 		if schema.propertyNames ~= nil then
 			-- could be used to validate non-string keys of Lua tables
 			for k in pairs(data) do
-				if not validate(schema.propertyNames, k, root) then
-					return false
+				if not validate(schema.propertyNames, k, root, sloc.."/propertyNames", iloc.."/"..tostring(k), errs) then
+					table.insert(errs, mkerr(sloc.."/propertyNames", iloc.."/"..tostring(k), "a property name did not match subschema"))
+					return false, errs;
 				end
 			end
 		end
@@ -357,8 +399,9 @@
 
 		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
+				if data[k] ~= nil and not validate(sub, data[k], root, sloc.."/"..tostring(k), iloc.."/"..tostring(k), errs) then
+					table.insert(errs, mkerr(sloc.."/"..tostring(k), iloc.."/"..tostring(k), "a property did not match subschema"))
+					return false, errs;
 				end
 				seen_properties[k] = true
 			end
@@ -369,8 +412,9 @@
 			for pattern, sub in pairs(schema.luaPatternProperties) do
 				for k in pairs(data) do
 					if k is string and k:match(pattern) then
-						if not validate(sub, data[k], root) then
-							return false
+						if not validate(sub, data[k], root, sloc.."/luaPatternProperties", iloc, errs) then
+							table.insert(errs, mkerr(sloc.."/luaPatternProperties/"..pattern, iloc.."/"..tostring(k), "a property did not match subschema"))
+							return false, errs;
 						end
 						seen_properties[k] = true
 					end
@@ -381,8 +425,9 @@
 		if schema.additionalProperties ~= nil then
 			for k, v in pairs(data) do
 				if not seen_properties[k as string] then
-					if not validate(schema.additionalProperties, v, root) then
-						return false
+					if not validate(schema.additionalProperties, v, root, sloc.."/additionalProperties", iloc.."/"..tostring(k), errs) then
+						table.insert(errs, mkerr(sloc.."/additionalProperties", iloc.."/"..tostring(k), "additional property did not match subschema"))
+						return false, errs;
 					end
 				end
 			end
@@ -390,8 +435,9 @@
 
 		if schema.dependentSchemas then
 			for k, sub in pairs(schema.dependentSchemas) do
-				if data[k] ~= nil and not validate(sub, data, root) then
-					return false
+				if data[k] ~= nil and not validate(sub, data, root, sloc.."/dependentSchemas/"..k, iloc, errs) then
+					table.insert(errs, mkerr(sloc.."/dependentSchemas", iloc.."/"..tostring(k), "did not match dependent subschema"))
+					return false, errs;
 				end
 			end
 		end
@@ -401,7 +447,8 @@
 			local values : { any : boolean } = {}
 			for _, v in pairs(data) do
 				if values[v] then
-					return false
+					table.insert(errs, mkerr(sloc.."/uniqueItems", iloc, "had duplicate items"))
+					return false, errs;
 				end
 				values[v] = true
 			end
@@ -412,18 +459,20 @@
 			for i, s in ipairs(schema.prefixItems) do
 				if data[i] == nil then
 					break
-				elseif validate(s, data[i], root) then
+				elseif validate(s, data[i], root, sloc.."/prefixItems/"..i, iloc.."/"..i, errs) then
 					p = i
 				else
-					return false
+					table.insert(errs, mkerr(sloc.."/prefixItems/"..i, iloc.."/"..tostring(i), "did not match subschema"))
+					return false, errs;
 				end
 			end
 		end
 
 		if schema.items ~= nil then
 			for i = p+1, #(data as {any}) do
-				if not validate(schema.items, data[i], root) then
-					return false
+				if not validate(schema.items, data[i], root, sloc, iloc.."/"..i, errs) then
+					table.insert(errs, mkerr(sloc.."/prefixItems/"..i, iloc.."/"..i, "did not match subschema"))
+					return false, errs;
 				end
 			end
 		end
@@ -431,12 +480,18 @@
 		if schema.contains ~= nil then
 			local found = 0
 			for i = 1, #(data as {any}) do
-				if validate(schema.contains, data[i], root) then
+				if validate(schema.contains, data[i], root, sloc.."/contains", iloc.."/"..i, errs) then
 					found = found + 1
+				else
+					table.insert(errs, mkerr(sloc.."/contains", iloc.."/"..i, "did not match subschema"))
 				end
 			end
-			if found < (schema.minContains or 1) or found > (schema.maxContains or math.huge) then
-				return false
+			if found < (schema.minContains or 1) then
+				table.insert(errs, mkerr(sloc.."/minContains", iloc, "too few matches"))
+				return false, errs;
+			elseif found > (schema.maxContains or math.huge) then
+				table.insert(errs, mkerr(sloc.."/maxContains", iloc, "too many matches"))
+				return false, errs;
 			end
 		end
 	end
--- a/util/jsonschema.lua	Sun Dec 03 15:46:33 2023 +0100
+++ b/util/jsonschema.lua	Sat Dec 02 21:45:38 2023 +0100
@@ -1,23 +1,20 @@
 -- This file is generated from teal-src/util/jsonschema.lua
 
-local m_type = function(n)
-	return type(n) == "number" and n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
-end;
+if not math.type then
+	require("prosody.util.mathcompat")
+end
 
 local utf8_enc = rawget(_G, "utf8") or require("prosody.util.encodings").utf8;
 local utf8_len = utf8_enc.len or function(s)
 	local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", "");
 	return count
 end;
+
 local json = require("prosody.util.json")
 local null = json.null;
 
 local pointer = require("prosody.util.jsonpointer")
 
-local json_type_name = json.json_type_name
-
-local schema_t = {}
-
 local json_schema_object = { xml_t = {} }
 
 local function simple_validate(schema, data)
@@ -28,7 +25,7 @@
 	elseif schema == "array" and type(data) == "table" then
 		return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "number")
 	elseif schema == "integer" then
-		return m_type(data) == schema
+		return math.type(data) == schema
 	elseif schema == "null" then
 		return data == null
 	elseif type(schema) == "table" then
@@ -43,26 +40,35 @@
 	end
 end
 
-local function validate(schema, data, root)
+local function mkerr(sloc, iloc, err)
+	return { schemaLocation = sloc; instanceLocation = iloc; error = err }
+end
+
+local function validate(schema, data, root, sloc, iloc, errs)
 	if type(schema) == "boolean" then
 		return schema
 	end
 
 	if root == nil then
 		root = schema
+		iloc = ""
+		sloc = ""
+		errs = {};
 	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
+			if not validate(referenced, data, root, schema["$ref"], iloc, errs) then
+				table.insert(errs, mkerr(sloc .. "/$ref", iloc, "Subschema failed validation"))
+				return false, errs
 			end
 		end
 	end
 
 	if not simple_validate(schema.type, data) then
-		return false
+		table.insert(errs, mkerr(sloc .. "/type", iloc, "unexpected type"));
+		return false, errs
 	end
 
 	if schema.type == "object" then
@@ -70,7 +76,8 @@
 
 			for k in pairs(data) do
 				if not (type(k) == "string") then
-					return false
+					table.insert(errs, mkerr(sloc .. "/type", iloc, "'object' had non-string keys"));
+					return false, errs
 				end
 			end
 		end
@@ -80,8 +87,9 @@
 		if type(data) == "table" then
 
 			for i in pairs(data) do
-				if not (m_type(i) == "integer") then
-					return false
+				if not (math.type(i) == "integer") then
+					table.insert(errs, mkerr(sloc .. "/type", iloc, "'array' had non-integer keys"));
+					return false, errs
 				end
 			end
 		end
@@ -97,113 +105,136 @@
 			end
 		end
 		if not match then
-			return false
+			table.insert(errs, mkerr(sloc .. "/enum", iloc, "not one of the enumerated values"));
+			return false, errs
 		end
 	end
 
 	if type(data) == "string" then
 		if schema.maxLength and utf8_len(data) > schema.maxLength then
-			return false
+			table.insert(errs, mkerr(sloc .. "/maxLength", iloc, "string too long"))
+			return false, errs
 		end
 		if schema.minLength and utf8_len(data) < schema.minLength then
-			return false
+			table.insert(errs, mkerr(sloc .. "/maxLength", iloc, "string too short"))
+			return false, errs
 		end
 		if schema.luaPattern and not data:match(schema.luaPattern) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/luaPattern", iloc, "string does not match pattern"))
+			return false, errs
 		end
 	end
 
 	if type(data) == "number" then
 		if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/luaPattern", iloc, "not a multiple"))
+			return false, errs
 		end
 
 		if schema.maximum and not (data <= schema.maximum) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/maximum", iloc, "number exceeds maximum"))
+			return false, errs
 		end
 
 		if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/exclusiveMaximum", iloc, "number exceeds exclusive maximum"))
+			return false, errs
 		end
 
 		if schema.minimum and not (data >= schema.minimum) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/minimum", iloc, "number below minimum"))
+			return false, errs
 		end
 
 		if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then
-			return false
+			table.insert(errs, mkerr(sloc .. "/exclusiveMinimum", iloc, "number below exclusive minimum"))
+			return false, errs
 		end
 	end
 
 	if schema.allOf then
-		for _, sub in ipairs(schema.allOf) do
-			if not validate(sub, data, root) then
-				return false
+		for i, sub in ipairs(schema.allOf) do
+			if not validate(sub, data, root, sloc .. "/allOf/" .. i, iloc, errs) then
+				table.insert(errs, mkerr(sloc .. "/allOf", iloc, "did not match all subschemas"))
+				return false, errs
 			end
 		end
 	end
 
 	if schema.oneOf then
 		local valid = 0
-		for _, sub in ipairs(schema.oneOf) do
-			if validate(sub, data, root) then
+		for i, sub in ipairs(schema.oneOf) do
+			if validate(sub, data, root, sloc .. "/oneOf" .. i, iloc, errs) then
 				valid = valid + 1
 			end
 		end
 		if valid ~= 1 then
-			return false
+			table.insert(errs, mkerr(sloc .. "/oneOf", iloc, "did not match exactly one subschema"))
+			return false, errs
 		end
 	end
 
 	if schema.anyOf then
 		local match = false
-		for _, sub in ipairs(schema.anyOf) do
-			if validate(sub, data, root) then
+		for i, sub in ipairs(schema.anyOf) do
+			if validate(sub, data, root, sloc .. "/anyOf/" .. i, iloc, errs) then
 				match = true
 				break
 			end
 		end
 		if not match then
-			return false
+			table.insert(errs, mkerr(sloc .. "/anyOf", iloc, "did not match any subschema"))
+			return false, errs
 		end
 	end
 
 	if schema["not"] then
-		if validate(schema["not"], data, root) then
-			return false
+		if validate(schema["not"], data, root, sloc .. "/not", iloc, errs) then
+			table.insert(errs, mkerr(sloc .. "/not", iloc, "did match subschema"))
+			return false, errs
 		end
 	end
 
 	if schema["if"] ~= nil then
-		if validate(schema["if"], data, root) then
+		if validate(schema["if"], data, root, sloc .. "/if", iloc, errs) then
 			if schema["then"] then
-				return validate(schema["then"], data, root)
+				if not validate(schema["then"], data, root, sloc .. "/then", iloc, errs) then
+					table.insert(errs, mkerr(sloc .. "/then", iloc, "did not match subschema"))
+					return false, errs
+				end
 			end
 		else
 			if schema["else"] then
-				return validate(schema["else"], data, root)
+				if not validate(schema["else"], data, root, sloc .. "/else", iloc, errs) then
+					table.insert(errs, mkerr(sloc .. "/else", iloc, "did not match subschema"))
+					return false, errs
+				end
 			end
 		end
 	end
 
 	if schema.const ~= nil and schema.const ~= data then
-		return false
+		table.insert(errs, mkerr(sloc .. "/const", iloc, "did not match constant value"))
+		return false, errs
 	end
 
 	if type(data) == "table" then
 
 		if schema.maxItems and #(data) > schema.maxItems then
-			return false
+			table.insert(errs, mkerr(sloc .. "/maxItems", iloc, "too many items"))
+			return false, errs
 		end
 
 		if schema.minItems and #(data) < schema.minItems then
-			return false
+			table.insert(errs, mkerr(sloc .. "/minItems", iloc, "too few items"))
+			return false, errs
 		end
 
 		if schema.required then
 			for _, k in ipairs(schema.required) do
 				if data[k] == nil then
-					return false
+					table.insert(errs, mkerr(sloc .. "/required", iloc .. "/" .. tostring(k), "missing required property"))
+					return false, errs
 				end
 			end
 		end
@@ -213,7 +244,8 @@
 				if data[k] ~= nil then
 					for _, req in ipairs(reqs) do
 						if data[req] == nil then
-							return false
+							table.insert(errs, mkerr(sloc .. "/dependentRequired", iloc, "missing dependent required property"))
+							return false, errs
 						end
 					end
 				end
@@ -221,9 +253,11 @@
 		end
 
 		if schema.propertyNames ~= nil then
+
 			for k in pairs(data) do
-				if not validate(schema.propertyNames, k, root) then
-					return false
+				if not validate(schema.propertyNames, k, root, sloc .. "/propertyNames", iloc .. "/" .. tostring(k), errs) then
+					table.insert(errs, mkerr(sloc .. "/propertyNames", iloc .. "/" .. tostring(k), "a property name did not match subschema"))
+					return false, errs
 				end
 			end
 		end
@@ -232,8 +266,9 @@
 
 		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
+				if data[k] ~= nil and not validate(sub, data[k], root, sloc .. "/" .. tostring(k), iloc .. "/" .. tostring(k), errs) then
+					table.insert(errs, mkerr(sloc .. "/" .. tostring(k), iloc .. "/" .. tostring(k), "a property did not match subschema"))
+					return false, errs
 				end
 				seen_properties[k] = true
 			end
@@ -244,8 +279,9 @@
 			for pattern, sub in pairs(schema.luaPatternProperties) do
 				for k in pairs(data) do
 					if type(k) == "string" and k:match(pattern) then
-						if not validate(sub, data[k], root) then
-							return false
+						if not validate(sub, data[k], root, sloc .. "/luaPatternProperties", iloc, errs) then
+							table.insert(errs, mkerr(sloc .. "/luaPatternProperties/" .. pattern, iloc .. "/" .. tostring(k), "a property did not match subschema"))
+							return false, errs
 						end
 						seen_properties[k] = true
 					end
@@ -256,8 +292,9 @@
 		if schema.additionalProperties ~= nil then
 			for k, v in pairs(data) do
 				if not seen_properties[k] then
-					if not validate(schema.additionalProperties, v, root) then
-						return false
+					if not validate(schema.additionalProperties, v, root, sloc .. "/additionalProperties", iloc .. "/" .. tostring(k), errs) then
+						table.insert(errs, mkerr(sloc .. "/additionalProperties", iloc .. "/" .. tostring(k), "additional property did not match subschema"))
+						return false, errs
 					end
 				end
 			end
@@ -265,8 +302,9 @@
 
 		if schema.dependentSchemas then
 			for k, sub in pairs(schema.dependentSchemas) do
-				if data[k] ~= nil and not validate(sub, data, root) then
-					return false
+				if data[k] ~= nil and not validate(sub, data, root, sloc .. "/dependentSchemas/" .. k, iloc, errs) then
+					table.insert(errs, mkerr(sloc .. "/dependentSchemas", iloc .. "/" .. tostring(k), "did not match dependent subschema"))
+					return false, errs
 				end
 			end
 		end
@@ -276,7 +314,8 @@
 			local values = {}
 			for _, v in pairs(data) do
 				if values[v] then
-					return false
+					table.insert(errs, mkerr(sloc .. "/uniqueItems", iloc, "had duplicate items"))
+					return false, errs
 				end
 				values[v] = true
 			end
@@ -287,18 +326,20 @@
 			for i, s in ipairs(schema.prefixItems) do
 				if data[i] == nil then
 					break
-				elseif validate(s, data[i], root) then
+				elseif validate(s, data[i], root, sloc .. "/prefixItems/" .. i, iloc .. "/" .. i, errs) then
 					p = i
 				else
-					return false
+					table.insert(errs, mkerr(sloc .. "/prefixItems/" .. i, iloc .. "/" .. tostring(i), "did not match subschema"))
+					return false, errs
 				end
 			end
 		end
 
 		if schema.items ~= nil then
 			for i = p + 1, #(data) do
-				if not validate(schema.items, data[i], root) then
-					return false
+				if not validate(schema.items, data[i], root, sloc, iloc .. "/" .. i, errs) then
+					table.insert(errs, mkerr(sloc .. "/prefixItems/" .. i, iloc .. "/" .. i, "did not match subschema"))
+					return false, errs
 				end
 			end
 		end
@@ -306,12 +347,18 @@
 		if schema.contains ~= nil then
 			local found = 0
 			for i = 1, #(data) do
-				if validate(schema.contains, data[i], root) then
+				if validate(schema.contains, data[i], root, sloc .. "/contains", iloc .. "/" .. i, errs) then
 					found = found + 1
+				else
+					table.insert(errs, mkerr(sloc .. "/contains", iloc .. "/" .. i, "did not match subschema"))
 				end
 			end
-			if found < (schema.minContains or 1) or found > (schema.maxContains or math.huge) then
-				return false
+			if found < (schema.minContains or 1) then
+				table.insert(errs, mkerr(sloc .. "/minContains", iloc, "too few matches"))
+				return false, errs
+			elseif found > (schema.maxContains or math.huge) then
+				table.insert(errs, mkerr(sloc .. "/maxContains", iloc, "too many matches"))
+				return false, errs
 			end
 		end
 	end