util.jsonschema: Implement the "contains" keyword
authorKim Alvefur <zash@zash.se>
Tue, 09 Mar 2021 02:35:00 +0100
changeset 11446 95f0d77175ca
parent 11445 75a280e6e046
child 11447 a526abef61e6
util.jsonschema: Implement the "contains" keyword And apparently I had mistaken this for an array
teal-src/util/jsonschema.tl
util/jsonschema.lua
--- a/teal-src/util/jsonschema.tl	Tue Mar 09 02:33:28 2021 +0100
+++ b/teal-src/util/jsonschema.tl	Tue Mar 09 02:35:00 2021 +0100
@@ -38,7 +38,7 @@
 
 	-- arrays
 	items : schema_t
-	contains : { schema_t }
+	contains : schema_t
 	maxItems : number
 	minItems : number
 	uniqueItems : boolean
@@ -285,6 +285,19 @@
 			end
 		end
 
+		if schema.contains then
+			local found = false
+			for i = 1, #data do
+				if validate(schema.contains, data[i]) then
+					found = true
+					break
+				end
+			end
+			if not found then
+				return false
+			end
+		end
+
 		return true
 	end
 	return false
--- a/util/jsonschema.lua	Tue Mar 09 02:33:28 2021 +0100
+++ b/util/jsonschema.lua	Tue Mar 09 02:35:00 2021 +0100
@@ -205,6 +205,19 @@
 			end
 		end
 
+		if schema.contains then
+			local found = false
+			for i = 1, #data do
+				if validate(schema.contains, data[i]) then
+					found = true
+					break
+				end
+			end
+			if not found then
+				return false
+			end
+		end
+
 		return true
 	end
 	return false