diff options
author | Kim Alvefur <zash@zash.se> | 2023-04-23 10:26:43 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2023-04-23 10:26:43 +0200 |
commit | 3621b8ce9168d833303bc8090302a77dcd76e1fd (patch) | |
tree | f1f18027700e857d72e506478ba7996e0985577a | |
parent | d78a32f136f4dc1d4c6fc839f43b93f98eae72fd (diff) | |
download | prosody-3621b8ce9168d833303bc8090302a77dcd76e1fd.tar.gz prosody-3621b8ce9168d833303bc8090302a77dcd76e1fd.zip |
util.jsonschema: Implement 'minContains' and 'maxContains'
-rw-r--r-- | spec/util_jsonschema_spec.lua | 2 | ||||
-rw-r--r-- | teal-src/prosody/util/jsonschema.tl | 11 | ||||
-rw-r--r-- | util/jsonschema.lua | 7 |
3 files changed, 8 insertions, 12 deletions
diff --git a/spec/util_jsonschema_spec.lua b/spec/util_jsonschema_spec.lua index 9d52a2fd..fa1afeca 100644 --- a/spec/util_jsonschema_spec.lua +++ b/spec/util_jsonschema_spec.lua @@ -22,10 +22,8 @@ local skip = { ["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", diff --git a/teal-src/prosody/util/jsonschema.tl b/teal-src/prosody/util/jsonschema.tl index 16099e76..e47b859c 100644 --- a/teal-src/prosody/util/jsonschema.tl +++ b/teal-src/prosody/util/jsonschema.tl @@ -84,8 +84,8 @@ local record json_schema_object maxItems : integer minItems : integer uniqueItems : boolean - maxContains : integer -- NYI - minContains : integer -- NYI + maxContains : integer + minContains : integer -- objects maxProperties : integer -- NYI @@ -429,14 +429,13 @@ function complex_validate (schema : json_schema_object, data : any, root : json_ 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 diff --git a/util/jsonschema.lua b/util/jsonschema.lua index 2a6d6bbf..51ad506f 100644 --- a/util/jsonschema.lua +++ b/util/jsonschema.lua @@ -305,14 +305,13 @@ function complex_validate(schema, data, root) 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 |