Changeset

13088:0fbb2b3fd4c0

util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
author Kim Alvefur <zash@zash.se>
date Sun, 23 Apr 2023 10:42:07 +0200
parents 13087:5d3e8a226840
children 13089:41598b7ec543
files spec/util_jsonschema_spec.lua teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 3 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_jsonschema_spec.lua	Sun Apr 23 10:26:43 2023 +0200
+++ b/spec/util_jsonschema_spec.lua	Sun Apr 23 10:42:07 2023 +0200
@@ -22,9 +22,7 @@
 	["dynamicRef.json"] = "NYI",
 	["enum.json:1:3"] = "deepcompare",
 	["id.json"] = "NYI",
-	["maxLength.json:0:4"] = "UTF-16",
 	["maxProperties.json"] = "NYI",
-	["minLength.json:0:4"] = "UTF-16",
 	["minProperties.json"] = "NYI",
 	["multipleOf.json:1"] = "multiples of IEEE 754 fractions",
 	["multipleOf.json:2"] = "multiples of IEEE 754 fractions",
--- a/teal-src/prosody/util/jsonschema.tl	Sun Apr 23 10:26:43 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sun Apr 23 10:42:07 2023 +0200
@@ -10,6 +10,13 @@
 
 if not math.type then require "prosody.util.mathcompat" end
 
+
+local utf8 = rawget(_G, "utf8") or require"prosody.util.encodings".utf8;
+local utf8_len = utf8.len or function(s)
+	local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", "");
+	return count;
+end;
+
 local json = require "prosody.util.json"
 local null = json.null;
 
@@ -220,10 +227,10 @@
 	-- XXX this is measured in byte, while JSON measures in ... bork
 	-- TODO use utf8.len?
 	if data is string then
-		if schema.maxLength and #data > schema.maxLength then
+		if schema.maxLength and utf8_len(data) > schema.maxLength then
 			return false
 		end
-		if schema.minLength and #data < schema.minLength then
+		if schema.minLength and utf8_len(data) < schema.minLength then
 			return false
 		end
 		if schema.luaPattern and not data:match(schema.luaPattern) then
--- a/util/jsonschema.lua	Sun Apr 23 10:26:43 2023 +0200
+++ b/util/jsonschema.lua	Sun Apr 23 10:42:07 2023 +0200
@@ -3,6 +3,12 @@
 local m_type = function(n)
 	return type(n) == "number" and n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
 end;
+
+local utf8 = rawget(_G, "utf8") or require("prosody.util.encodings").utf8;
+local utf8_len = utf8.len or function(s)
+	local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", "");
+	return count
+end;
 local json = require("prosody.util.json")
 local null = json.null;
 
@@ -103,10 +109,10 @@
 	end
 
 	if type(data) == "string" then
-		if schema.maxLength and #data > schema.maxLength then
+		if schema.maxLength and utf8_len(data) > schema.maxLength then
 			return false
 		end
-		if schema.minLength and #data < schema.minLength then
+		if schema.minLength and utf8_len(data) < schema.minLength then
 			return false
 		end
 		if schema.luaPattern and not data:match(schema.luaPattern) then