aboutsummaryrefslogtreecommitdiffstats
path: root/util/jsonschema.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/jsonschema.lua')
-rw-r--r--util/jsonschema.lua78
1 files changed, 58 insertions, 20 deletions
diff --git a/util/jsonschema.lua b/util/jsonschema.lua
index eafa8b7c..eab2d817 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_enc = rawget(_G, "utf8") or require("prosody.util.encodings").utf8;
+local utf8_len = utf8_enc.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
@@ -37,17 +43,10 @@ local function simple_validate(schema, data)
end
end
-local complex_validate
-
local function validate(schema, data, root)
if type(schema) == "boolean" then
return schema
- else
- return complex_validate(schema, data, root)
end
-end
-
-function complex_validate(schema, data, root)
if root == nil then
root = schema
@@ -103,10 +102,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 #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
return false
end
end
@@ -190,11 +192,11 @@ function complex_validate(schema, data, root)
if type(data) == "table" then
- if schema.maxItems and #data > schema.maxItems then
+ if schema.maxItems and #(data) > schema.maxItems then
return false
end
- if schema.minItems and #data < schema.minItems then
+ if schema.minItems and #(data) < schema.minItems then
return false
end
@@ -206,6 +208,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 +228,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 +263,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 = {}
@@ -257,7 +296,7 @@ function complex_validate(schema, data, root)
end
if schema.items ~= nil then
- for i = p + 1, #data do
+ for i = p + 1, #(data) do
if not validate(schema.items, data[i], root) then
return false
end
@@ -265,14 +304,13 @@ function complex_validate(schema, data, root)
end
if schema.contains ~= nil then
- local found = false
- for i = 1, #data do
+ 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