Software /
code /
prosody
Annotate
util/jsonschema.lua @ 11442:95f0d77175ca
util.jsonschema: Implement the "contains" keyword
And apparently I had mistaken this for an array
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Mar 2021 02:35:00 +0100 |
parent | 11441:75a280e6e046 |
child | 11443:a526abef61e6 |
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) |
11441
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
60 if type(schema) == "boolean" then |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
61 return schema |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
62 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 if type(schema) == "string" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 return simple_validate(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if type(schema) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 if schema.allOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 for _, sub in ipairs(schema.allOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 if not validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if schema.oneOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 local valid = 0 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 for _, sub in ipairs(schema.oneOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 valid = 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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return valid == 1 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 if schema.anyOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 for _, sub in ipairs(schema.anyOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 return true |
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 end |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 if schema["not"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 if validate(schema["not"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 if schema["if"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 if validate(schema["if"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 if schema["then"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 return validate(schema["then"], data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 if schema["else"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 return validate(schema["else"], data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 if not simple_validate(schema.type, data) then |
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.const ~= nil and schema.const ~= data then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 if schema.enum ~= nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 for _, v in ipairs(schema.enum) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 if v == data then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 return true |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 local validator = type_validators[schema.type] |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 if not validator then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 return true |
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 return validator(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 end |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 type_validators.table = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 if type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 if schema.maxItems and #data > schema.maxItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 if schema.minItems and #data < schema.minItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 if schema.required then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 for _, k in ipairs(schema.required) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 if data[k] == nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 end |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 if schema.properties then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 for k, s in pairs(schema.properties) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 if data[k] ~= nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 if not validate(s, data[k]) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 if schema.additionalProperties then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 for k, v in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 if type(k) == "string" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 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
|
172 if not validate(schema.additionalProperties, v) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 elseif schema.properties then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 for k in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 if type(k) == "string" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 if schema.properties[k] == nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 if schema.uniqueItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 local values = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 for _, v in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 if values[v] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 values[v] = true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
200 if schema.items then |
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
201 for i = 1, #data do |
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
202 if not validate(schema.items, data[i]) then |
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
203 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 end |
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 |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
208 if schema.contains then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
209 local found = false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
210 for i = 1, #data do |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
211 if validate(schema.contains, data[i]) then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
212 found = true |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
213 break |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
214 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
215 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
216 if not found then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
217 return false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
218 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
219 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
220 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 end |
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 type_validators.object = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 if type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 for k in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 if not (type(k) == "string") then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 return type_validators.table(schema, data) |
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 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 type_validators.array = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 if type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 for i in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 if not (type(i) == "number") then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 return type_validators.table(schema, data) |
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 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 return {validate = validate; schema_t = schema_t} |