util.jsonschema: Implement 'minContains' and 'maxContains'
authorKim Alvefur <zash@zash.se>
Sun, 23 Apr 2023 10:26:43 +0200
changeset 13091 5d3e8a226840
parent 13090 42ea593bfa8d
child 13092 0fbb2b3fd4c0
util.jsonschema: Implement 'minContains' and 'maxContains'
spec/util_jsonschema_spec.lua
teal-src/prosody/util/jsonschema.tl
util/jsonschema.lua
--- a/spec/util_jsonschema_spec.lua	Sat Apr 22 13:30:19 2023 +0200
+++ b/spec/util_jsonschema_spec.lua	Sun Apr 23 10:26:43 2023 +0200
@@ -22,10 +22,8 @@
 	["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",
--- a/teal-src/prosody/util/jsonschema.tl	Sat Apr 22 13:30:19 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sun Apr 23 10:26:43 2023 +0200
@@ -84,8 +84,8 @@
 	maxItems : integer
 	minItems : integer
 	uniqueItems : boolean
-	maxContains : integer -- NYI
-	minContains : integer -- NYI
+	maxContains : integer
+	minContains : integer
 
 	-- objects
 	maxProperties : integer -- NYI
@@ -429,14 +429,13 @@
 		end
 
 		if schema.contains ~= nil then
-			local found = false
+			local found = 0
 			for i = 1, #data do
 				if validate(schema.contains, data[i], root) then
-					found = true
-					break
+					found = found + 1
 				end
 			end
-			if not found then
+			if found < (schema.minContains or 1) or found > (schema.maxContains or math.huge) then
 				return false
 			end
 		end
--- a/util/jsonschema.lua	Sat Apr 22 13:30:19 2023 +0200
+++ b/util/jsonschema.lua	Sun Apr 23 10:26:43 2023 +0200
@@ -305,14 +305,13 @@
 		end
 
 		if schema.contains ~= nil then
-			local found = false
+			local found = 0
 			for i = 1, #data do
 				if validate(schema.contains, data[i], root) then
-					found = true
-					break
+					found = found + 1
 				end
 			end
-			if not found then
+			if found < (schema.minContains or 1) or found > (schema.maxContains or math.huge) then
 				return false
 			end
 		end