aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-03-09 02:36:08 +0100
committerKim Alvefur <zash@zash.se>2021-03-09 02:36:08 +0100
commitda85d37bcb847acf53031811277392afe2a17ed2 (patch)
treebc2e53aa87813257f5a5aa10c5699dee4b017b34
parentce4040e1091705d94bb46c59dcd0cc16edde702e (diff)
downloadprosody-da85d37bcb847acf53031811277392afe2a17ed2.tar.gz
prosody-da85d37bcb847acf53031811277392afe2a17ed2.zip
util.jsonschema: Implement the "prefixItems" keyword
This may have been what got me confused about "items" being an array.
-rw-r--r--teal-src/util/jsonschema.tl14
-rw-r--r--util/jsonschema.lua13
2 files changed, 25 insertions, 2 deletions
diff --git a/teal-src/util/jsonschema.tl b/teal-src/util/jsonschema.tl
index 3f5b0f26..89a9b724 100644
--- a/teal-src/util/jsonschema.tl
+++ b/teal-src/util/jsonschema.tl
@@ -37,6 +37,7 @@ local record schema_t
format : string
-- arrays
+ prefixItems : { schema_t }
items : schema_t
contains : schema_t
maxItems : number
@@ -277,8 +278,19 @@ type_validators.table = function (schema : schema_t, data : any) : boolean
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
diff --git a/util/jsonschema.lua b/util/jsonschema.lua
index 4ace2d2d..97068c5c 100644
--- a/util/jsonschema.lua
+++ b/util/jsonschema.lua
@@ -197,8 +197,19 @@ type_validators.table = function(schema, data)
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