# HG changeset patch # User Kim Alvefur # Date 1682238403 -7200 # Node ID 5d3e8a2268404a35fb2847ebb0a38e6eb99daee5 # Parent 42ea593bfa8ddcbab3596dc920974dc390c17ac9 util.jsonschema: Implement 'minContains' and 'maxContains' diff -r 42ea593bfa8d -r 5d3e8a226840 spec/util_jsonschema_spec.lua --- a/spec/util_jsonschema_spec.lua Sat Apr 22 13:30:19 2023 +0200 +++ b/spec/util_jsonschema_spec.lua Sun Apr 23 10:26:43 2023 +0200 @@ -22,10 +22,8 @@ ["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 -r 42ea593bfa8d -r 5d3e8a226840 teal-src/prosody/util/jsonschema.tl --- a/teal-src/prosody/util/jsonschema.tl Sat Apr 22 13:30:19 2023 +0200 +++ b/teal-src/prosody/util/jsonschema.tl Sun Apr 23 10:26:43 2023 +0200 @@ -84,8 +84,8 @@ maxItems : integer minItems : integer uniqueItems : boolean - maxContains : integer -- NYI - minContains : integer -- NYI + maxContains : integer + minContains : integer -- objects maxProperties : integer -- NYI @@ -429,14 +429,13 @@ 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 -r 42ea593bfa8d -r 5d3e8a226840 util/jsonschema.lua --- a/util/jsonschema.lua Sat Apr 22 13:30:19 2023 +0200 +++ b/util/jsonschema.lua Sun Apr 23 10:26:43 2023 +0200 @@ -305,14 +305,13 @@ 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