Software /
code /
prosody
Annotate
teal-src/util/jsonschema.tl @ 11445:c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
This is a bit cleaner, I think
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Mar 2021 02:41:47 +0100 |
parent | 11444:b3a903032611 |
child | 11446:58c534bac798 |
rev | line source |
---|---|
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local record schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 enum type_e |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 "null" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 "boolean" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 "object" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 "array" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 "number" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 "string" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 "integer" |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 type : type_e |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 enum : { any } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 const : any |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 allOf : { schema_t } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 anyOf : { schema_t } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 oneOf : { schema_t } |
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 ["not"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 ["if"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 ["then"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 ["else"] : schema_t |
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 -- numbers |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 multipleOf : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 maximum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 exclusiveMaximum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 minimum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 exclusiveMinimum : number |
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 -- strings |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 maxLength : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 minLength : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 pattern : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 format : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- arrays |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
40 prefixItems : { schema_t } |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
41 items : schema_t |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
42 contains : schema_t |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 maxItems : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 minItems : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 uniqueItems : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 maxContains : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 minContains : number |
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 -- objects |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 properties : { string : schema_t | type_e } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 maxProperties : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 minProperties : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 required : { string } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 dependentRequired : { string : { string } } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 additionalProperties: schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 patternProperties: schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 -- xml |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 record xml_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 name : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 namespace : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 prefix : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 attribute : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 wrapped : boolean |
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 -- nonstantard, maybe in the future |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 text : boolean |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
68 x_name_is_value : boolean |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
69 x_single_attribute : string |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 xml : xml_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 -- descriptive |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 title : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 description : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 deprecated : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 readOnly : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 writeOnly : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local type_e = schema_t.type_e |
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 -- TODO validator function per schema property |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 local type_validators : { type_e : function (schema_t, any) : boolean } = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 local function simple_validate(schema : type_e, data : any) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 if schema == "object" and data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 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
|
90 elseif schema == "array" and data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 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
|
92 elseif schema == "integer" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 return math.type(data) == schema |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 return type(data) == schema |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 type_validators.string = function (schema : schema_t, data : any) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 -- XXX this is measured in byte, while JSON measures in ... bork |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 -- TODO use utf8.len? |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 if data is string then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 if schema.maxLength and #data > schema.maxLength 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 if schema.minLength and #data < schema.minLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 return false |
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 return true |
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 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 type_validators.number = function (schema : schema_t, data : number) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 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
|
116 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 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
|
120 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 if schema.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) then |
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 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
|
128 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 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
|
132 return false |
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 true |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 type_validators.integer = type_validators.number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
11441
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
140 local function validate(schema : schema_t | type_e | boolean, data : any) : boolean |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
141 if schema is boolean then |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
142 return schema |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
143 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 if schema is type_e then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 return simple_validate(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 if schema is schema_t then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 if schema.allOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 for _, sub in ipairs(schema.allOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 if not validate(sub, data) 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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 return true |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 if schema.oneOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 local valid = 0 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 for _, sub in ipairs(schema.oneOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 valid = valid + 1 |
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 return valid == 1 |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 if schema.anyOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 for _, sub in ipairs(schema.anyOf) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 if validate(sub, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 return true |
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 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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 if schema["not"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 if validate(schema["not"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 if schema["if"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 if validate(schema["if"], data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 if schema["then"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 return validate(schema["then"], data) |
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 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 if schema["else"] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 return validate(schema["else"], data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 if not simple_validate(schema.type, data) then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 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
|
199 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 if schema.enum ~= nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 for _, v in ipairs(schema.enum) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 if v == data then |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 local validator = type_validators[schema.type] |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 if not validator then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 return true |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 return validator(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 type_validators.table = function (schema : schema_t, data : any) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 if data is table then |
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 if schema.maxItems and #data > schema.maxItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 if schema.minItems and #data < schema.minItems 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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 if schema.required then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 for _, k in ipairs(schema.required) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 if data[k] == nil then |
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 end |
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 if schema.properties then |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
240 local additional : schema_t | boolean = schema.additionalProperties or true |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
241 for k, v in pairs(data) do |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
242 local s = schema.properties[k as string] or additional as schema_t |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
243 if not validate(s, v) then |
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
244 return false |
11434
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 |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
247 elseif schema.additionalProperties then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 for k, v in pairs(data) do |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
249 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
|
250 return false |
11434
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 if schema.uniqueItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 -- only works for scalars, would need to deep-compare for objects/arrays/tables |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 local values : { any : boolean } = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 for _, v in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 if values[v] then |
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 values[v] = true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
266 local p = 0 |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
267 if schema.prefixItems then |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
268 for i, s in ipairs(schema.prefixItems) do |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
269 if validate(s, data[i]) then |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
270 p = i |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
271 else |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
272 return false |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
273 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
274 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
275 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
276 |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
277 if schema.items then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
278 for i = p+1, #data do |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
279 if not validate(schema.items, data[i]) then |
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
280 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
285 if schema.contains then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
286 local found = false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
287 for i = 1, #data do |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
288 if validate(schema.contains, data[i]) then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
289 found = true |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
290 break |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
291 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
292 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
293 if not found then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
294 return false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
295 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
296 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
297 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 type_validators.object = function (schema : schema_t, data : any) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 if data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 for k in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 if not k is string then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 return type_validators.table(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 type_validators.array = function (schema : schema_t, data : any) : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 if data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 -- just check that there the keys are all numbers |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 for i in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 if not i is number then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 return type_validators.table(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 return { |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 validate = validate; |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 schema_t = schema_t; |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 } |