util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
authorKim Alvefur <zash@zash.se>
Sat, 22 Apr 2023 12:48:51 +0200
changeset 13089 0e17cb78264f
parent 13088 87f646e353cf
child 13090 42ea593bfa8d
util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern' Like 'pattern' but uses Lua patterns instead of Regular Expressions, since only a subset of regex are also valid Lua patterns.
teal-src/prosody/util/jsonschema.tl
util/jsonschema.lua
--- a/teal-src/prosody/util/jsonschema.tl	Sat Apr 22 12:14:29 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sat Apr 22 12:48:51 2023 +0200
@@ -98,6 +98,7 @@
 
 	-- for Lua
 	luaPatternProperties: { string : schema_t }
+	luaPattern : string
 
 	-- xml
 	record xml_t
@@ -225,6 +226,9 @@
 		if schema.minLength and #data < schema.minLength then
 			return false
 		end
+		if schema.luaPattern and not data:match(schema.luaPattern) then
+			return false
+		end
 	end
 
 	if data is number then
--- a/util/jsonschema.lua	Sat Apr 22 12:14:29 2023 +0200
+++ b/util/jsonschema.lua	Sat Apr 22 12:48:51 2023 +0200
@@ -109,6 +109,9 @@
 		if schema.minLength and #data < schema.minLength then
 			return false
 		end
+		if schema.luaPattern and not data:match(schema.luaPattern) then
+			return false
+		end
 	end
 
 	if type(data) == "number" then