Changeset

13084:87f646e353cf

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.
author Kim Alvefur <zash@zash.se>
date Sat, 22 Apr 2023 12:14:29 +0200 (20 months ago)
parents 13083:962a746842a0
children 13085:0e17cb78264f
files teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 2 files changed, 42 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/teal-src/prosody/util/jsonschema.tl	Sat Apr 22 12:45:58 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sat Apr 22 12:14:29 2023 +0200
@@ -96,6 +96,9 @@
 	-- semantic format
 	format : string
 
+	-- for Lua
+	luaPatternProperties: { string : schema_t }
+
 	-- xml
 	record xml_t
 		name : string
@@ -339,17 +342,37 @@
 			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
--- a/util/jsonschema.lua	Sat Apr 22 12:45:58 2023 +0200
+++ b/util/jsonschema.lua	Sat Apr 22 12:14:29 2023 +0200
@@ -226,17 +226,34 @@
 			end
 		end
 
+		local seen_properties = {}
+
 		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
+
+			for pattern, sub in pairs(schema.luaPatternProperties) do
+				for k in pairs(data) do
+					if type(k) == "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] == nil then
+				if not seen_properties[k] then
 					if not validate(schema.additionalProperties, v, root) then
 						return false
 					end