util.jsonschema: Implement the "prefixItems" keyword
authorKim Alvefur <zash@zash.se>
Tue, 09 Mar 2021 02:36:08 +0100
changeset 11447 a526abef61e6
parent 11446 95f0d77175ca
child 11448 b3a903032611
util.jsonschema: Implement the "prefixItems" keyword This may have been what got me confused about "items" being an array.
teal-src/util/jsonschema.tl
util/jsonschema.lua
--- a/teal-src/util/jsonschema.tl	Tue Mar 09 02:35:00 2021 +0100
+++ b/teal-src/util/jsonschema.tl	Tue Mar 09 02:36:08 2021 +0100
@@ -37,6 +37,7 @@
 	format : string
 
 	-- arrays
+	prefixItems : { schema_t }
 	items : schema_t
 	contains : schema_t
 	maxItems : number
@@ -277,8 +278,19 @@
 			return true
 		end
 
+		local p = 0
+		if schema.prefixItems then
+			for i, s in ipairs(schema.prefixItems) do
+				if validate(s, data[i]) then
+					p = i
+				else
+					return false
+				end
+			end
+		end
+
 		if schema.items then
-			for i = 1, #data do
+			for i = p+1, #data do
 				if not validate(schema.items, data[i]) then
 					return false
 				end
--- a/util/jsonschema.lua	Tue Mar 09 02:35:00 2021 +0100
+++ b/util/jsonschema.lua	Tue Mar 09 02:36:08 2021 +0100
@@ -197,8 +197,19 @@
 			return true
 		end
 
+		local p = 0
+		if schema.prefixItems then
+			for i, s in ipairs(schema.prefixItems) do
+				if validate(s, data[i]) then
+					p = i
+				else
+					return false
+				end
+			end
+		end
+
 		if schema.items then
-			for i = 1, #data do
+			for i = p + 1, #data do
 				if not validate(schema.items, data[i]) then
 					return false
 				end