Annotate

util/jsonschema.lua @ 13270:14bbfb2cc8dd default tip

lint: Teach luacheck about module:once Silence warning for using this introduced in 9c62ffbdf2ae
author Kim Alvefur <zash@zash.se>
date Sun, 15 Oct 2023 16:43:14 +0200
parent 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 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
47 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
48 return schema
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
49 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
50
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
51 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
52 root = schema
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
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
55 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
56 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
57 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
58 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
59 return false
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 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
62 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
63
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
64 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
65 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
66 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
67
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
68 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
69 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
70
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
71 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
72 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
73 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
74 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
75 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
76 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
77 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
78
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
79 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
80 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
81
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
82 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
83 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
84 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
85 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
86 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
87 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
88 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
89
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
90 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
91 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
92 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
93 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
94
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
95 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
96 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
97 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
98 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
99 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
100 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
101 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
102 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 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
105 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
106 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
13088
0fbb2b3fd4c0 util.jsonschema: Fix UTF-8ness of 'minLength' and 'maxLength'
Kim Alvefur <zash@zash.se>
parents: 13087
diff changeset
108 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
109 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 end
13085
0e17cb78264f util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Kim Alvefur <zash@zash.se>
parents: 13084
diff changeset
111 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
112 return false
0e17cb78264f util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Kim Alvefur <zash@zash.se>
parents: 13084
diff changeset
113 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
115
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
116 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
117 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
118 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
119 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
120
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
121 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
122 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
123 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
124
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
125 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
126 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
127 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
129 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
130 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
131 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
132
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
133 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
134 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
135 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
136 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
137
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
138 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
139 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
140 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
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
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
146 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
147 local valid = 0
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
148 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
149 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
150 valid = valid + 1
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
151 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
152 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
153 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
154 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
155 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
158 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
159 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
160 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
161 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
162 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
163 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
164 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
165 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
166 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
167 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
168 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
171 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
172 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
173 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
174 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
177 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
178 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
179 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
180 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
181 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
182 else
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
183 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
184 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
185 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
186 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
187 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
188
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
189 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
190 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
195 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
196 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
199 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
200 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210
12988
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
211 if schema.dependentRequired then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
212 for k, reqs in pairs(schema.dependentRequired) do
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
213 if data[k] ~= nil then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
214 for _, req in ipairs(reqs) do
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
215 if data[req] == nil then
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
216 return false
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
217 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
218 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
219 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
220 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
221 end
8592770be63a util.jsonschema: Implement 'dependentRequired'
Kim Alvefur <zash@zash.se>
parents: 12975
diff changeset
222
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
223 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
224 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
225 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
226 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
229 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
230
13084
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
231 local seen_properties = {}
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
232
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
233 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
234 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
235 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
236 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
237 end
13084
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
238 seen_properties[k] = true
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
239 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
240 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
241
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
242 if schema.luaPatternProperties then
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
243
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
244 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
245 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
246 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
247 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
248 return false
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
249 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
250 seen_properties[k] = true
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
251 end
87f646e353cf util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties'
Kim Alvefur <zash@zash.se>
parents: 12989
diff changeset
252 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
253 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
254 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
255
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
256 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
257 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
258 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
259 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
260 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
261 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265
12989
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
266 if schema.dependentSchemas then
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
267 for k, sub in pairs(schema.dependentSchemas) do
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
268 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
269 return false
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
270 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
271 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
272 end
dee080c2441e util.jsonschema: Implement 'dependentSchemas'
Kim Alvefur <zash@zash.se>
parents: 12988
diff changeset
273
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 if schema.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 local values = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 if values[v] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
285 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
286 if schema.prefixItems ~= nil then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
287 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
288 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
289 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
290 elseif validate(s, data[i], root) then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
291 p = i
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
292 else
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
293 return false
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
294 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
295 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
296 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
297
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
298 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
299 for i = p + 1, #(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
300 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
301 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
306 if schema.contains ~= nil then
13087
5d3e8a226840 util.jsonschema: Implement 'minContains' and 'maxContains'
Kim Alvefur <zash@zash.se>
parents: 13085
diff changeset
307 local found = 0
13162
6140aa67c618 util.jsonschema: Silence Teal warnings about counting items in tables
Kim Alvefur <zash@zash.se>
parents: 13088
diff changeset
308 for i = 1, #(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
309 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
310 found = found + 1
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
311 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
312 end
13087
5d3e8a226840 util.jsonschema: Implement 'minContains' and 'maxContains'
Kim Alvefur <zash@zash.se>
parents: 13085
diff changeset
313 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
314 return false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
315 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
316 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12500
diff changeset
319 return true
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
322 json_schema_object.validate = validate;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
323
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
324 return json_schema_object