aboutsummaryrefslogtreecommitdiffstats
path: root/util/jsonschema.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/jsonschema.lua')
-rw-r--r--util/jsonschema.lua63
1 files changed, 54 insertions, 9 deletions
diff --git a/util/jsonschema.lua b/util/jsonschema.lua
index eafa8b7c..c67fb744 100644
--- a/util/jsonschema.lua
+++ b/util/jsonschema.lua
@@ -3,10 +3,16 @@
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 json = require("util.json")
+
+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;
-local pointer = require("util.jsonpointer")
+local pointer = require("prosody.util.jsonpointer")
local json_type_name = json.json_type_name
@@ -103,10 +109,13 @@ function complex_validate(schema, data, root)
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 utf8_len(data) < schema.minLength then
return false
end
- if schema.minLength and #data < schema.minLength then
+ if schema.luaPattern and not data:match(schema.luaPattern) then
return false
end
end
@@ -206,6 +215,18 @@ function complex_validate(schema, data, root)
end
end
+ if schema.dependentRequired then
+ for k, reqs in pairs(schema.dependentRequired) do
+ if data[k] ~= nil then
+ for _, req in ipairs(reqs) do
+ if data[req] == nil then
+ return false
+ end
+ end
+ end
+ end
+ end
+
if schema.propertyNames ~= nil then
for k in pairs(data) do
if not validate(schema.propertyNames, k, root) then
@@ -214,17 +235,34 @@ function complex_validate(schema, data, root)
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
@@ -232,6 +270,14 @@ function complex_validate(schema, data, root)
end
end
+ if schema.dependentSchemas then
+ for k, sub in pairs(schema.dependentSchemas) do
+ if data[k] ~= nil and not validate(sub, data, root) then
+ return false
+ end
+ end
+ end
+
if schema.uniqueItems then
local values = {}
@@ -265,14 +311,13 @@ function complex_validate(schema, data, root)
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