Annotate

util/jsonschema.lua @ 12565:2e0c7f5cd8f8

util.hashes: Return OpenSSL error messages on failure With luck, might contain more details than just "failed"
author Kim Alvefur <zash@zash.se>
date Fri, 24 Jun 2022 15:33:04 +0200
parent 12500:88e1b94105ae
child 12579:ca6a43fe0231
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12500
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
1 local m_type = math.type or function (n)
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
2 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
3 end;
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
4 local json = require("util.json")
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
5 local null = json.null;
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
7 local pointer = require("util.jsonpointer")
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
8
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
9 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
10
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
11 local schema_t = {}
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
12
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
13 local json_schema_object = {xml_t = {}}
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local type_validators = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local function simple_validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 if schema == "object" and type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 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
20 elseif schema == "array" and type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 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
22 elseif schema == "integer" then
12500
88e1b94105ae util.jsonschema: Lua <5.3 compat here too
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
23 return m_type(data) == schema
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
24 elseif schema == "null" then
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
25 return data == null
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 type_validators.string = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 if type(data) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 if schema.maxLength and #data > schema.maxLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if schema.minLength and #data < schema.minLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 type_validators.number = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if schema.multipleOf and data % schema.multipleOf ~= 0 then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if schema.maximum and not (data <= schema.maximum) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if schema.minimum and not (data >= schema.minimum) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 type_validators.integer = type_validators.number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
71 local function validate(schema, data, root)
11441
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
72 if type(schema) == "boolean" then
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
73 return schema
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
74 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 if type(schema) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return simple_validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 if type(schema) == "table" then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
79 if root == nil then
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
80 root = schema
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
81 end
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
82 if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
83 local referenced = pointer.resolve(root, schema["$ref"]:sub(2))
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
84 if referenced ~= nil then
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
85 return validate(referenced, data, root)
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
86 end
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
87 end
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
88
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if schema.allOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 for _, sub in ipairs(schema.allOf) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
91 if not validate(sub, data, root) then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 if schema.oneOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 local valid = 0
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 for _, sub in ipairs(schema.oneOf) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
101 if validate(sub, data, root) then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 valid = valid + 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 return valid == 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 if schema.anyOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 for _, sub in ipairs(schema.anyOf) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
110 if validate(sub, data, root) then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 if schema["not"] then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
118 if validate(schema["not"], data, root) then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 if schema["if"] then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
124 if validate(schema["if"], data, root) then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 if schema["then"] then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
126 return validate(schema["then"], data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 if schema["else"] then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
130 return validate(schema["else"], data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 if schema.const ~= nil and schema.const ~= data then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138
11447
7ad137fe665b util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents: 11446
diff changeset
139 if schema["enum"] ~= nil then
7ad137fe665b util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents: 11446
diff changeset
140 for _, v in ipairs(schema["enum"]) do
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 if v == data then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 end
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 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147
11448
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
148 if schema.type then
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
149 if not simple_validate(schema.type, data) then
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
150 return false
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
151 end
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
152
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
153 local validator = type_validators[schema.type]
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
154 if validator then
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
155 return validator(schema, data, root)
11448
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
156 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 end
11448
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
158 return true
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
162 type_validators.table = function(schema, data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 if schema.maxItems and #data > schema.maxItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if schema.minItems and #data < schema.minItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 if schema.properties then
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
182 local additional = schema.additionalProperties or true
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
183 for k, v in pairs(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
184 if schema.propertyNames and not validate(schema.propertyNames, k, root) then
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
185 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
186 end
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
187 local s = schema.properties[k] or additional
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
188 if not validate(s, v, root) then
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
189 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 end
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
192 elseif schema.additionalProperties then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 for k, v in pairs(data) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
194 if schema.propertyNames and not validate(schema.propertyNames, k, root) then
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
195 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
196 end
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
197 if not validate(schema.additionalProperties, v, root) then
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
198 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
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.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local values = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 if values[v] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 return false
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 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
214 local p = 0
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
215 if schema.prefixItems then
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
216 for i, s in ipairs(schema.prefixItems) do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
217 if validate(s, data[i], root) then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
218 p = i
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
219 else
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
220 return false
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
221 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
222 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
223 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
224
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
225 if schema.items then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
226 for i = p + 1, #data do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
227 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
228 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
233 if schema.contains then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
234 local found = false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
235 for i = 1, #data do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
236 if validate(schema.contains, data[i], root) then
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
237 found = true
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
238 break
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
239 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
240 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
241 if not found then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
242 return false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
243 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
244 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
245
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
251 type_validators.object = function(schema, data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 for k in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 if not (type(k) == "string") then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
259 return type_validators.table(schema, data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 return false
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
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
264 type_validators.array = function(schema, data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 for i in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 if not (type(i) == "number") then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 return false
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
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
273 return type_validators.table(schema, data, root)
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
278 json_schema_object.validate = validate;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
279
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11448
diff changeset
280 return json_schema_object