Software /
code /
prosody
Annotate
teal-src/util/jsonschema.tl @ 12600:3d3a0c4e2662
Merge 0.12->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 29 Jul 2022 17:10:31 +0200 |
parent | 12579:ca6a43fe0231 |
child | 12782:8815d3090928 |
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
19 local type schema_t = boolean | json_schema_object |
11460
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
25 type : json_type_name | { 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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
50 pattern : string -- NYI |
11434
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
60 maxContains : integer -- NYI |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
61 minContains : integer -- NYI |
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 } |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
65 maxProperties : integer -- NYI |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
66 minProperties : integer -- NYI |
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
70 patternProperties: schema_t -- NYI |
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
102 local function simple_validate(schema : json_type_name | { json_type_name }, data : any) : boolean |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
103 if schema == nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
104 return true |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
105 elseif schema == "object" and data is table then |
11434
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
113 elseif schema is { json_type_name } then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
114 for _, one in ipairs(schema as { json_type_name }) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
115 if simple_validate(one, data) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
116 return true |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
117 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
118 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
119 return false |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 else |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 return type(data) == schema |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
125 local complex_validate : function ( json_schema_object, any, json_schema_object ) : boolean |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
126 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
127 local function validate (schema : schema_t, data : any, root : json_schema_object) : boolean |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
128 if schema is boolean then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
129 return schema |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
130 else |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
131 return complex_validate(schema, data, root) |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
132 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
133 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
134 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
135 function complex_validate (schema : json_schema_object, data : any, root : json_schema_object) : boolean |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
136 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
137 if root == nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
138 root = schema |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
139 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
140 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
141 if schema["$ref"] and schema["$ref"]:sub(1,1) == "#" then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
142 local referenced = pointer.resolve(root as table, schema["$ref"]:sub(2)) as schema_t |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
143 if referenced ~= nil and referenced ~= root and referenced ~= schema then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
144 if not validate(referenced, data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
145 return false; |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
146 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
147 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
148 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
149 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
150 if not simple_validate(schema.type, data) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
151 return false; |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
152 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
153 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
154 if schema.type == "object" then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
155 if data is table then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
156 -- just check that there the keys are all strings |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
157 for k in pairs(data) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
158 if not k is string then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
159 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
160 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
161 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
162 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
163 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
164 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
165 if schema.type == "array" then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
166 if data is table then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
167 -- just check that there the keys are all numbers |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
168 for i in pairs(data) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
169 if not i is integer then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
170 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
171 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
172 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
173 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
174 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
175 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
176 if schema["enum"] ~= nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
177 local match = false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
178 for _, v in ipairs(schema["enum"]) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
179 if v == data then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
180 -- FIXME supposed to do deep-compare |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
181 match = true |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
182 break |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
183 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
184 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
185 if not match then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
186 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
187 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
188 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
189 |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 -- 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
|
191 -- TODO use utf8.len? |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 if data is string then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 if schema.maxLength and #data > schema.maxLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 if schema.minLength and #data < schema.minLength then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 end |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
200 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
201 if data is number then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
202 if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
203 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
204 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
205 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
206 if schema.maximum and not ( data <= schema.maximum ) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
207 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
208 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
209 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
210 if schema.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
211 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
212 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
214 if schema.minimum and not ( data >= schema.minimum ) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
215 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
216 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
217 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
218 if schema.exclusiveMinimum and not ( data > schema.exclusiveMinimum ) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
219 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
220 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
221 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
222 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
223 if schema.allOf then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
224 for _, sub in ipairs(schema.allOf) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
225 if not validate(sub, data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
226 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
227 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
228 end |
11434
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 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
231 if schema.oneOf then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
232 local valid = 0 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
233 for _, sub in ipairs(schema.oneOf) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
234 if validate(sub, data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
235 valid = valid + 1 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
236 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
237 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
238 if valid ~= 1 then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
239 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
240 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
243 if schema.anyOf then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
244 local match = false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
245 for _, sub in ipairs(schema.anyOf) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
246 if validate(sub, data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
247 match = true |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
248 break |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
249 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
250 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
251 if not match then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
252 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
253 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
256 if schema["not"] then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
257 if validate(schema["not"], data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
258 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
259 end |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
262 if schema["if"] ~= nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
263 if validate(schema["if"], data, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
264 if schema["then"] then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
265 return validate(schema["then"], data, root) |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
266 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
267 else |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
268 if schema["else"] then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
269 return validate(schema["else"], data, root) |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
270 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
271 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
272 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
273 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
274 if schema.const ~= nil and schema.const ~= data then |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 if data is table then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 if schema.maxItems and #data > schema.maxItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 return false |
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 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 if schema.minItems and #data < schema.minItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 if schema.required then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 for _, k in ipairs(schema.required) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 if data[k] == nil then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
296 if schema.propertyNames ~= nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
297 for k in pairs(data) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
298 if not validate(schema.propertyNames, k, root) then |
11445
c73744fa3bdf
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents:
11444
diff
changeset
|
299 return false |
11434
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 end |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
302 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
303 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
304 if schema.properties then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
305 for k, sub in pairs(schema.properties) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
306 if data[k] ~= nil and not validate(sub, data[k], root) then |
11446
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
307 return false |
58c534bac798
util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents:
11445
diff
changeset
|
308 end |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
309 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
310 end |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
311 |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
312 if schema.additionalProperties ~= nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
313 for k, v in pairs(data) do |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
314 if schema.properties == nil or schema.properties[k as string] == nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
315 if not validate(schema.additionalProperties, v, root) then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
316 return false |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
317 end |
11434
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 if schema.uniqueItems then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 -- 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
|
324 local values : { any : boolean } = {} |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 for _, v in pairs(data) do |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 if values[v] then |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 return false |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 values[v] = true |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
333 local p = 0 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
334 if schema.prefixItems ~= nil then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
335 for i, s in ipairs(schema.prefixItems) do |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
336 if data[i] == nil then |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
337 break |
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
338 elseif validate(s, data[i], root) then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
339 p = i |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
340 else |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
341 return false |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
342 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
343 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
344 end |
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
345 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
346 if schema.items ~= nil then |
11443
a526abef61e6
util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents:
11442
diff
changeset
|
347 for i = p+1, #data do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
348 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
|
349 return false |
11434
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 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
354 if schema.contains ~= nil then |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
355 local found = false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
356 for i = 1, #data do |
12132
4ff0d33dfb2b
util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents:
11460
diff
changeset
|
357 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
|
358 found = true |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
359 break |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
360 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
361 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
362 if not found then |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
363 return false |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
364 end |
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
365 end |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
366 end |
11442
95f0d77175ca
util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents:
11441
diff
changeset
|
367 |
12579
ca6a43fe0231
util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents:
12132
diff
changeset
|
368 return true; |
11434
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 end |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 |
66d4067bdfb2
util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 |
11460
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
372 json_schema_object.validate = validate; |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
373 |
a8b4e04bc044
util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
374 return json_schema_object; |