Annotate

util/jsonschema.lua @ 13163:f43d04653bcf

util.jsonschema: Silence Teal warnings about utf8 library Teal worries that we redefine the global. Also that the fallback was missing type information.
author Kim Alvefur <zash@zash.se>
date Sat, 17 Jun 2023 17:17:44 +0200
parent 13162:6140aa67c618
child 13164:1aa83a5667f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
1 -- This file is generated from teal-src/util/jsonschema.lua
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
2
12760
ce1a0f9bf25a util.jsonschema: Use same integer/float logic on Lua 5.2 and 5.3
Kim Alvefur <zash@zash.se>
parents: 12759
diff changeset
3 local m_type = function(n)
ce1a0f9bf25a util.jsonschema: Use same integer/float logic on Lua 5.2 and 5.3
Kim Alvefur <zash@zash.se>
parents: 12759
diff changeset
4 return type(n) == "number" and n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
12500
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
5 end;
13088
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
6
13163
f43d04653bcf util.jsonschema: Silence Teal warnings about utf8 library
Kim Alvefur <zash@zash.se>
parents: 13162
diff changeset
7 local utf8_enc = rawget(_G, "utf8") or require("prosody.util.encodings").utf8;
f43d04653bcf util.jsonschema: Silence Teal warnings about utf8 library
Kim Alvefur <zash@zash.se>
parents: 13162
diff changeset
8 local utf8_len = utf8_enc.len or function(s)
13088
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
9 local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", "");
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
10 return count
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
11 end;
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12760
diff changeset
12 local json = require("prosody.util.json")
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
13 local null = json.null;
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12760
diff changeset
15 local pointer = require("prosody.util.jsonpointer")
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
16
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
17 local json_type_name = json.json_type_name
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
18
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
19 local schema_t = {}
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
20
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
21 local json_schema_object = { xml_t = {} }
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local function simple_validate(schema, data)
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
24 if schema == nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
25 return true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
26 elseif schema == "object" and type(data) == "table" then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "string")
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 elseif schema == "array" and type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "number")
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 elseif schema == "integer" then
12500
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
31 return m_type(data) == schema
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
32 elseif schema == "null" then
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
33 return data == null
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
34 elseif type(schema) == "table" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
35 for _, one in ipairs(schema) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
36 if simple_validate(one, data) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
37 return true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
38 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
39 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
40 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 return type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
46 local complex_validate
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
47
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
48 local function validate(schema, data, root)
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
49 if type(schema) == "boolean" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
50 return schema
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
51 else
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
52 return complex_validate(schema, data, root)
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
53 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
54 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
55
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
56 function complex_validate(schema, data, root)
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
57
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
58 if root == nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
59 root = schema
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
60 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
61
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
62 if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
63 local referenced = pointer.resolve(root, schema["$ref"]:sub(2))
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
64 if referenced ~= nil and referenced ~= root and referenced ~= schema then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
65 if not validate(referenced, data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
66 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
67 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
68 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
69 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
70
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
71 if not simple_validate(schema.type, data) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
72 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
73 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
74
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
75 if schema.type == "object" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
76 if type(data) == "table" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
77
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
78 for k in pairs(data) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
79 if not (type(k) == "string") then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
80 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
81 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
82 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
83 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
84 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
85
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
86 if schema.type == "array" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
87 if type(data) == "table" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
88
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
89 for i in pairs(data) do
12759
ec54fe0003d5 util.jsonschema: Fix Lua 5.2 integer compat
Kim Alvefur <zash@zash.se>
parents: 12579
diff changeset
90 if not (m_type(i) == "integer") then
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
91 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
92 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
93 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
94 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
95 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
96
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
97 if schema["enum"] ~= nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
98 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
99 for _, v in ipairs(schema["enum"]) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
100 if v == data then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
101
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
102 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
103 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
104 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
105 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
106 if not match then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
107 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
108 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
109 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 if type(data) == "string" then
13088
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
112 if schema.maxLength and utf8_len(data) > schema.maxLength then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
13088
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
115 if schema.minLength and utf8_len(data) < schema.minLength then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end
13085
0e17cb78264f util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Kim Alvefur <zash@zash.se>
parents: 13084
diff changeset
118 if schema.luaPattern and not data:match(schema.luaPattern) then
0e17cb78264f util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Kim Alvefur <zash@zash.se>
parents: 13084
diff changeset
119 return false
0e17cb78264f util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Kim Alvefur <zash@zash.se>
parents: 13084
diff changeset
120 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
122
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
123 if type(data) == "number" then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
124 if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
125 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
126 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
127
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
128 if schema.maximum and not (data <= schema.maximum) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
129 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
130 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
131
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
132 if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
133 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
134 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
136 if schema.minimum and not (data >= schema.minimum) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
137 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
138 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
139
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
140 if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
141 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
142 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
143 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
144
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
145 if schema.allOf then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
146 for _, sub in ipairs(schema.allOf) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
147 if not validate(sub, data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
148 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
149 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
150 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
153 if schema.oneOf then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
154 local valid = 0
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
155 for _, sub in ipairs(schema.oneOf) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
156 if validate(sub, data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
157 valid = valid + 1
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
158 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
159 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
160 if valid ~= 1 then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
161 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
162 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
165 if schema.anyOf then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
166 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
167 for _, sub in ipairs(schema.anyOf) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
168 if validate(sub, data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
169 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
170 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
171 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
172 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
173 if not match then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
174 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
175 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
178 if schema["not"] then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
179 if validate(schema["not"], data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
180 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
181 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
184 if schema["if"] ~= nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
185 if validate(schema["if"], data, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
186 if schema["then"] then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
187 return validate(schema["then"], data, root)
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
188 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
189 else
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
190 if schema["else"] then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
191 return validate(schema["else"], data, root)
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
192 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
193 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
194 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
195
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
196 if schema.const ~= nil and schema.const ~= data then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
202 if schema.maxItems and #(data) > schema.maxItems then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
206 if schema.minItems and #(data) < schema.minItems then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217
12988
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
218 if schema.dependentRequired then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
219 for k, reqs in pairs(schema.dependentRequired) do
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
220 if data[k] ~= nil then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
221 for _, req in ipairs(reqs) do
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
222 if data[req] == nil then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
223 return false
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
224 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
225 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
226 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
227 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
228 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
229
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
230 if schema.propertyNames ~= nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
231 for k in pairs(data) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
232 if not validate(schema.propertyNames, k, root) then
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
233 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
236 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
237
13084
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
238 local seen_properties = {}
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
239
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
240 if schema.properties then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
241 for k, sub in pairs(schema.properties) do
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
242 if data[k] ~= nil and not validate(sub, data[k], root) then
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
243 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
244 end
13084
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
245 seen_properties[k] = true
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
246 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
247 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
248
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
249 if schema.luaPatternProperties then
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
250
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
251 for pattern, sub in pairs(schema.luaPatternProperties) do
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
252 for k in pairs(data) do
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
253 if type(k) == "string" and k:match(pattern) then
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
254 if not validate(sub, data[k], root) then
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
255 return false
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
256 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
257 seen_properties[k] = true
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
258 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
259 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
260 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
261 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
262
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
263 if schema.additionalProperties ~= nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
264 for k, v in pairs(data) do
13084
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
265 if not seen_properties[k] then
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
266 if not validate(schema.additionalProperties, v, root) then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
267 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
268 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272
12989
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
273 if schema.dependentSchemas then
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
274 for k, sub in pairs(schema.dependentSchemas) do
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
275 if data[k] ~= nil and not validate(sub, data, root) then
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
276 return false
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
277 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
278 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
279 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
280
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 if schema.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 local values = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 if values[v] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
292 local p = 0
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
293 if schema.prefixItems ~= nil then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
294 for i, s in ipairs(schema.prefixItems) do
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
295 if data[i] == nil then
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
296 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
297 elseif validate(s, data[i], root) then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
298 p = i
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
299 else
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
300 return false
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
301 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
302 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
303 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
304
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
305 if schema.items ~= nil then
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
306 for i = p + 1, #(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
307 if not validate(schema.items, data[i], root) then
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
308 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
313 if schema.contains ~= nil then
13087
5d3e8a226840 util.jsonschema: Implement 'minContains' and 'maxContains'
Kim Alvefur <zash@zash.se>
parents: 13085
diff changeset
314 local found = 0
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
315 for i = 1, #(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
316 if validate(schema.contains, data[i], root) then
13087
5d3e8a226840 util.jsonschema: Implement 'minContains' and 'maxContains'
Kim Alvefur <zash@zash.se>
parents: 13085
diff changeset
317 found = found + 1
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
318 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
319 end
13087
5d3e8a226840 util.jsonschema: Implement 'minContains' and 'maxContains'
Kim Alvefur <zash@zash.se>
parents: 13085
diff changeset
320 if found < (schema.minContains or 1) or found > (schema.maxContains or math.huge) then
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
321 return false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
322 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
323 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
326 return true
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
329 json_schema_object.validate = validate;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
330
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
331 return json_schema_object