util.jsonschema: Implement the "prefixItems" keyword
This may have been what got me confused about "items" being an array.
--- 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