From 1dad83d28eafc84d2253eadf8ed58d6caefc78df Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2023 12:14:29 +0200 Subject: util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties' Previous version of this patch used 'patternProperties' but that would only work with simpler ECMA-262 regular expressions are also valid Lua patterns. --- teal-src/prosody/util/jsonschema.tl | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'teal-src/prosody/util') diff --git a/teal-src/prosody/util/jsonschema.tl b/teal-src/prosody/util/jsonschema.tl index 7cf6d259..75cfab92 100644 --- a/teal-src/prosody/util/jsonschema.tl +++ b/teal-src/prosody/util/jsonschema.tl @@ -96,6 +96,9 @@ local record json_schema_object -- semantic format format : string + -- for Lua + luaPatternProperties: { string : schema_t } + -- xml record xml_t name : string @@ -339,17 +342,37 @@ function complex_validate (schema : json_schema_object, data : any, root : json_ end end + -- additionalProperties applies to properties not validated by properties + -- or patternProperties, so we must keep track of properties validated by + -- the later + local seen_properties : { string : boolean } = {} + if schema.properties then for k, sub in pairs(schema.properties) do if data[k] ~= nil and not validate(sub, data[k], root) then return false end + seen_properties[k] = true + end + end + + if schema.luaPatternProperties then + -- like patternProperties, but Lua patterns + for pattern, sub in pairs(schema.luaPatternProperties) do + for k in pairs(data) do + if k is string and k:match(pattern) then + if not validate(sub, data[k], root) then + return false + end + seen_properties[k] = true + end + end end end if schema.additionalProperties ~= nil then for k, v in pairs(data) do - if schema.properties == nil or schema.properties[k as string] == nil then + if not seen_properties[k as string] then if not validate(schema.additionalProperties, v, root) then return false end -- cgit v1.2.3