Changeset

13087:5d3e8a226840

util.jsonschema: Implement 'minContains' and 'maxContains'
author Kim Alvefur <zash@zash.se>
date Sun, 23 Apr 2023 10:26:43 +0200
parents 13086:42ea593bfa8d
children 13088:0fbb2b3fd4c0
files spec/util_jsonschema_spec.lua teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 3 files changed, 8 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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