Software /
code /
prosody
Annotate
util/jsonschema.lua @ 11845:97c1399720c2
util.argparse: Add test for #1691
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 12 Oct 2021 14:54:04 +0200 |
parent | 11460:a8b4e04bc044 |
child | 12132:4ff0d33dfb2b |
rev | line source |
---|---|
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
1 local json = require("util.json") |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
2 local null = json.null; |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
4 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
|
5 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
6 local schema_t = {} |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
7 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
8 local json_schema_object = {xml_t = {}} |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local type_validators = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local function simple_validate(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 if schema == "object" and type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 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
|
15 elseif schema == "array" and type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 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
|
17 elseif schema == "integer" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 return math.type(data) == schema |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
19 elseif schema == "null" then |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
20 return data == null |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 return type(data) == schema |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 type_validators.string = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 if type(data) == "string" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 if schema.maxLength and #data > schema.maxLength then |
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 if schema.minLength and #data < schema.minLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 return true |
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 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 type_validators.number = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 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
|
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 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
|
46 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 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
|
50 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 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
|
54 return false |
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 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
|
58 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return true |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 type_validators.integer = type_validators.number |
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 local function validate(schema, data) |
11441
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
67 if type(schema) == "boolean" then |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
68 return schema |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
69 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 if type(schema) == "string" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 return simple_validate(schema, data) |
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 if type(schema) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 if schema.allOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 for _, sub in ipairs(schema.allOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if not validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 return false |
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 true |
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.oneOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 local valid = 0 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 for _, sub in ipairs(schema.oneOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 valid = valid + 1 |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 return valid == 1 |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 if schema.anyOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 for _, sub in ipairs(schema.anyOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 end |
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 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 if schema["not"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 if validate(schema["not"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 return false |
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 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["if"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 if validate(schema["if"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 if schema["then"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 return validate(schema["then"], data) |
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 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 if schema["else"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 return validate(schema["else"], data) |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 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
|
121 return false |
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 |
11447
7ad137fe665b
util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents:
11446
diff
changeset
|
124 if schema["enum"] ~= nil then |
7ad137fe665b
util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents:
11446
diff
changeset
|
125 for _, v in ipairs(schema["enum"]) do |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 if v == data then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 return true |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 return false |
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 |
11448
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
133 if schema.type then |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
134 if not simple_validate(schema.type, data) then |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
135 return false |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
136 end |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
137 |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
138 local validator = type_validators[schema.type] |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
139 if validator then |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
140 return validator(schema, data) |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
141 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
11448
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
143 return true |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 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 type_validators.table = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 if type(data) == "table" then |
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.maxItems and #data > schema.maxItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 if schema.minItems and #data < schema.minItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 return false |
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.required then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 for _, k in ipairs(schema.required) 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 return false |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 if schema.properties then |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
167 local additional = schema.additionalProperties or true |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
168 for k, v in pairs(data) do |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
169 if schema.propertyNames and not validate(schema.propertyNames, k) then |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
170 return false |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
171 end |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
172 local s = schema.properties[k] or additional |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
173 if not validate(s, v) then |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
174 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
177 elseif schema.additionalProperties then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 for k, v in pairs(data) do |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
179 if schema.propertyNames and not validate(schema.propertyNames, k) then |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
180 return false |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
181 end |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
182 if not validate(schema.additionalProperties, v) then |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
183 return false |
11434
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
199 local p = 0 |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
200 if schema.prefixItems then |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
201 for i, s in ipairs(schema.prefixItems) do |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
202 if validate(s, data[i]) then |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
203 p = i |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
204 else |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
205 return false |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
206 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
207 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
208 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
209 |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
210 if schema.items then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
211 for i = p + 1, #data do |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
212 if not validate(schema.items, data[i]) then |
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
213 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
218 if schema.contains then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
219 local found = false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
220 for i = 1, #data do |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
221 if validate(schema.contains, data[i]) then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
222 found = true |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
223 break |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
224 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
225 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
226 if not found then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
227 return false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
228 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
229 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
230 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 return true |
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 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 type_validators.object = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 if type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 for k in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 if not (type(k) == "string") then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 return type_validators.table(schema, data) |
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 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 type_validators.array = function(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 if type(data) == "table" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 for i in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 if not (type(i) == "number") then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 end |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 return type_validators.table(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
263 json_schema_object.validate = validate; |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
264 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
265 return json_schema_object |