Software /
code /
prosody
Annotate
teal-src/util/jsonschema.tl @ 12225:e9f34a04067e
mod_admin_shell: Add help section about stats
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 29 Jan 2022 14:58:37 +0100 |
parent | 12132:4ff0d33dfb2b |
child | 12579:ca6a43fe0231 |
rev | line source |
---|---|
11449
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
1 -- Copyright (C) 2021 Kim Alvefur |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
2 -- |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
3 -- This project is MIT/X11 licensed. Please see the |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
4 -- COPYING file in the source package for more information. |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
5 -- |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
6 -- Based on |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
7 -- https://json-schema.org/draft/2020-12/json-schema-core.html |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
8 -- https://json-schema.org/draft/2020-12/json-schema-validation.html |
dabd1fae0540
util.jsonschema: Add copyright-header
Kim Alvefur <zash@zash.se>
parents:
11448
diff
changeset
|
9 -- |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
11 local json = require"util.json" |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
12 local null = json.null; |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
13 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
14 local pointer = require "util.jsonpointer" |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
15 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
16 local type json_type_name = json.json_type_name |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
18 -- json_type_name here is non-standard |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
19 local type schema_t = boolean | json_type_name | json_schema_object |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
20 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
21 local record json_schema_object |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
22 type json_type_name = json.json_type_name |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
23 type schema_object = json_schema_object |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
24 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
25 type : json_type_name |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 enum : { any } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 const : any |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 allOf : { schema_t } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 anyOf : { schema_t } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 oneOf : { schema_t } |
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 ["not"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 ["if"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 ["then"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 ["else"] : schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
38 ["$ref"] : string |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
39 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 -- numbers |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 multipleOf : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 maximum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 exclusiveMaximum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 minimum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 exclusiveMinimum : number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 -- strings |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
48 maxLength : integer |
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
49 minLength : integer |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 pattern : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 format : string |
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 -- arrays |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
54 prefixItems : { schema_t } |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
55 items : schema_t |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
56 contains : schema_t |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
57 maxItems : integer |
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
58 minItems : integer |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 uniqueItems : boolean |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
60 maxContains : integer |
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
61 minContains : integer |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 -- objects |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
64 properties : { string : schema_t } |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
65 maxProperties : integer |
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11449
diff
changeset
|
66 minProperties : integer |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 required : { string } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 dependentRequired : { string : { string } } |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 additionalProperties: schema_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 patternProperties: schema_t |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
71 propertyNames : schema_t |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 -- xml |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 record xml_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 name : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 namespace : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 prefix : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 attribute : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 wrapped : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 -- nonstantard, maybe in the future |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 text : boolean |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11434
diff
changeset
|
83 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
|
84 x_single_attribute : string |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end |
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 xml : xml_t |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 -- descriptive |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 title : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 description : string |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 deprecated : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 readOnly : boolean |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 writeOnly : boolean |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
95 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
96 -- methods |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
97 validate : function ( schema_t, any, json_schema_object ) : boolean |
11434
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 -- TODO validator function per schema property |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
102 local type_validators : { json_type_name : function (schema_t, any, json_schema_object) : boolean } = {} |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
104 local function simple_validate(schema : json_type_name, data : any) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 if schema == "object" and data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 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
|
107 elseif schema == "array" and data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 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
|
109 elseif schema == "integer" then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 return math.type(data) == schema |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
111 elseif schema == "null" then |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
112 return data == null |
11434
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 return type(data) == schema |
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
118 type_validators.string = function (schema : json_schema_object, data : any) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 -- 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
|
120 -- TODO use utf8.len? |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 if data is string then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 if schema.maxLength and #data > schema.maxLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 if schema.minLength and #data < schema.minLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 return true |
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 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
133 type_validators.number = function (schema : json_schema_object, data : number) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 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
|
135 return false |
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 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
|
139 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 end |
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.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) 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.minimum and not ( data >= schema.minimum ) 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.exclusiveMinimum and not ( data > schema.exclusiveMinimum ) 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 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 type_validators.integer = type_validators.number |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
159 local function validate(schema : schema_t, data : any, root : json_schema_object) : boolean |
11441
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
160 if schema is boolean then |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
161 return schema |
75a280e6e046
util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents:
11440
diff
changeset
|
162 end |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
163 if schema is json_type_name then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 return simple_validate(schema, data) |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 end |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
166 if schema is json_schema_object then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
167 if root == nil then |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
168 root = schema |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
169 end |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
170 if schema["$ref"] and schema["$ref"]:sub(1,1) == "#" then |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
171 local referenced = pointer.resolve(root as table, schema["$ref"]:sub(2)) as schema_t |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
172 if referenced ~= nil then |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
173 return validate(referenced, data, root); |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
174 end |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
175 end |
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
176 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 if schema.allOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 for _, sub in ipairs(schema.allOf) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
179 if not validate(sub, data, root) then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 return true |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 if schema.oneOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 local valid = 0 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 for _, sub in ipairs(schema.oneOf) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
189 if validate(sub, data, root) then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 valid = valid + 1 |
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 return valid == 1 |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 if schema.anyOf then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 for _, sub in ipairs(schema.anyOf) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
198 if validate(sub, data, root) then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 if schema["not"] then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
206 if validate(schema["not"], data, root) then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 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 if schema["if"] then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
212 if validate(schema["if"], data, root) then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 if schema["then"] then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
214 return validate(schema["then"], data, root) |
11434
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 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 if schema["else"] then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
218 return validate(schema["else"], data, root) |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 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
|
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 |
11447
7ad137fe665b
util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents:
11446
diff
changeset
|
227 if schema["enum"] ~= nil then |
7ad137fe665b
util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents:
11446
diff
changeset
|
228 for _, v in ipairs(schema["enum"]) do |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 if v == data then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 return true |
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 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 |
11448
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
236 if schema.type then |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
237 if not simple_validate(schema.type, data) then |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
238 return false |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
239 end |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
240 |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
241 local validator = type_validators[schema.type] |
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
242 if validator then |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
243 return validator(schema, data, root) |
11448
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
244 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 end |
11448
1d84b54ba0d7
util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents:
11447
diff
changeset
|
246 return true |
11434
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
250 type_validators.table = function (schema : json_schema_object, data : any, root : json_schema_object) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 if data is table then |
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 if schema.maxItems and #data > schema.maxItems 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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 if schema.minItems and #data < schema.minItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 if schema.required then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 for _, k in ipairs(schema.required) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 if data[k] == nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 if schema.properties then |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
270 local additional : schema_t = schema.additionalProperties or true |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
271 for k, v in pairs(data) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
272 if schema.propertyNames and not validate(schema.propertyNames, k, root) then |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
273 return false |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
274 end |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
275 local s = schema.properties[k as string] or additional |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
276 if not validate(s, v, root) then |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
277 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 end |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
280 elseif schema.additionalProperties then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 for k, v in pairs(data) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
282 if schema.propertyNames and not validate(schema.propertyNames, k, root) then |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
283 return false |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
284 end |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
285 if not validate(schema.additionalProperties, v, root) then |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
286 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 if schema.uniqueItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 -- 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
|
293 local values : { any : boolean } = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 for _, v in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 if values[v] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 values[v] = 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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
302 local p = 0 |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
303 if schema.prefixItems then |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
304 for i, s in ipairs(schema.prefixItems) do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
305 if validate(s, data[i], root) then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
306 p = i |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
307 else |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
308 return false |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
309 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
310 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
311 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
312 |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
313 if schema.items then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
314 for i = p+1, #data do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
315 if not validate(schema.items, data[i], root) then |
11440
d5288c99bb5a
util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
316 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
321 if schema.contains then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
322 local found = false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
323 for i = 1, #data do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
324 if validate(schema.contains, data[i], root) then |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
325 found = true |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
326 break |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
327 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
328 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
329 if not found then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
330 return false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
331 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
332 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
333 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 return true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
339 type_validators.object = function (schema : schema_t, data : any, root : json_schema_object) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 if data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 for k in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 if not k is string then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
347 return type_validators.table(schema, data, root) |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
352 type_validators.array = function (schema : schema_t, data : any, root : json_schema_object) : boolean |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 if data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 -- 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
|
356 for i in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 if not i is number then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
362 return type_validators.table(schema, data, root) |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
367 json_schema_object.validate = validate; |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
368 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
369 return json_schema_object; |