Annotate

util/jsonschema.lua @ 11440:d5288c99bb5a

util.jsonschema: Correct "items" keyword Upon re-reading the JSON Schema spec, I found that 'items' wasn't a union of an array of schemas or a single schema, not sure where I got that from.
author Kim Alvefur <zash@zash.se>
date Tue, 09 Mar 2021 02:26:05 +0100
parent 11434:66d4067bdfb2
child 11441:75a280e6e046
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local schema_t = {xml_t = {}}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local type_e = schema_t.type_e
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local type_validators = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local function simple_validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 if schema == "object" and type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 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
10 elseif schema == "array" and type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 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
12 elseif schema == "integer" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 return math.type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 return type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 type_validators.string = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 if type(data) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 if schema.maxLength and #data > schema.maxLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 if schema.minLength and #data < schema.minLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 return true
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 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
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 type_validators.number = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 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
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 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
39 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 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
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.exclusiveMinimum and not (data > schema.exclusiveMinimum) 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 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 type_validators.integer = type_validators.number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local function validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if type(schema) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 return simple_validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if type(schema) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if schema.allOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 for _, sub in ipairs(schema.allOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if not validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if schema.oneOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 local valid = 0
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 for _, sub in ipairs(schema.oneOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 if validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 valid = valid + 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 return valid == 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if schema.anyOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 for _, sub in ipairs(schema.anyOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 if validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if schema["not"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if validate(schema["not"], data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
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["if"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 if validate(schema["if"], data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 if schema["then"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 return validate(schema["then"], data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 if schema["else"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 return validate(schema["else"], data)
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 if not simple_validate(schema.type, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 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
115 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 if schema.enum ~= nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 for _, v in ipairs(schema.enum) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 if v == data then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 local validator = type_validators[schema.type]
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 if not validator then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 return validator(schema, data)
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 type_validators.table = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 if schema.maxItems and #data > schema.maxItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 if schema.minItems and #data < schema.minItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 return false
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 if schema.properties then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 for k, s in pairs(schema.properties) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 if data[k] ~= nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 if not validate(s, data[k]) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 return false
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 if schema.additionalProperties then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 for k, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 if type(k) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 if not (schema.properties and schema.properties[k]) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if not validate(schema.additionalProperties, v) 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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 elseif schema.properties then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 for k in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if type(k) == "string" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 if schema.properties[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 if schema.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local values = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 if values[v] then
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 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
197 if schema.items then
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
198 for i = 1, #data do
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
199 if not validate(schema.items, data[i]) then
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
200 return false
11434
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 end
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 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
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 type_validators.object = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 for k in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 if not (type(k) == "string") then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 return type_validators.table(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 type_validators.array = function(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 if type(data) == "table" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 for i in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 if not (type(i) == "number") then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 return type_validators.table(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 return {validate = validate; schema_t = schema_t}