Annotate

teal-src/util/jsonschema.tl @ 11563:0983653cbfdf

prosodyctl: Report OpenSSL version
author Kim Alvefur <zash@zash.se>
date Wed, 12 May 2021 01:11:42 +0200
parent 11460:a8b4e04bc044
child 12132:4ff0d33dfb2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
14 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
15
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
16 -- json_type_name here is non-standard
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
17 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
18
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
19 local record json_schema_object
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
20 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
21 type schema_object = json_schema_object
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
22
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
23 type : json_type_name
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 enum : { any }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 const : any
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 allOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 anyOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 oneOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 ["not"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 ["if"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 ["then"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 ["else"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- numbers
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 multipleOf : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 maximum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 exclusiveMaximum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 minimum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 exclusiveMinimum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 -- strings
11459
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
44 maxLength : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
45 minLength : integer
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 pattern : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 format : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 -- arrays
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
50 prefixItems : { schema_t }
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
51 items : schema_t
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
52 contains : schema_t
11459
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
53 maxItems : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
54 minItems : integer
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 uniqueItems : boolean
11459
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
56 maxContains : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
57 minContains : integer
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 -- objects
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
60 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
61 maxProperties : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
62 minProperties : integer
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 required : { string }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 dependentRequired : { string : { string } }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 additionalProperties: schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 patternProperties: schema_t
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
67 propertyNames : schema_t
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 -- xml
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 record xml_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 name : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 namespace : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 prefix : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 attribute : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 wrapped : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 -- nonstantard, maybe in the future
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 text : boolean
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
79 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
80 x_single_attribute : string
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 xml : xml_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- descriptive
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 title : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 description : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 deprecated : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 readOnly : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 writeOnly : boolean
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
91
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
92 -- methods
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
93 validate : function ( schema_t, any) : boolean
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 -- TODO validator function per schema property
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
98 local type_validators : { json_type_name : function (schema_t, any) : boolean } = {}
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
100 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
101 if schema == "object" and data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 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
103 elseif schema == "array" and data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 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
105 elseif schema == "integer" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return math.type(data) == schema
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
107 elseif schema == "null" then
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
108 return data == null
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 return type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
114 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
115 -- 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
116 -- TODO use utf8.len?
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 if data is string then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 if schema.maxLength and #data > schema.maxLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 if schema.minLength and #data < schema.minLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 return false
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 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 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
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
129 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
130 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
131 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 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
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.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) 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.minimum and not ( data >= schema.minimum ) 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.exclusiveMinimum and not ( data > schema.exclusiveMinimum ) 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 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 type_validators.integer = type_validators.number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
155 local function validate(schema : schema_t, data : any) : boolean
11441
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
156 if schema is boolean then
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
157 return schema
75a280e6e046 util.jsonschema: Allow a boolean as schema
Kim Alvefur <zash@zash.se>
parents: 11440
diff changeset
158 end
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
159 if schema is json_type_name then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 return simple_validate(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 end
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
162 if schema is json_schema_object then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 if schema.allOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 for _, sub in ipairs(schema.allOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 if not validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 if schema.oneOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 local valid = 0
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 for _, sub in ipairs(schema.oneOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 if validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 valid = valid + 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 return valid == 1
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 if schema.anyOf then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 for _, sub in ipairs(schema.anyOf) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 if validate(sub, data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 if schema["not"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 if validate(schema["not"], data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 if schema["if"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 if validate(schema["if"], data) then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 if schema["then"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 return validate(schema["then"], data)
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 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 if schema["else"] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 return validate(schema["else"], data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 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
210 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212
11447
7ad137fe665b util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents: 11446
diff changeset
213 if schema["enum"] ~= nil then
7ad137fe665b util.jsonschema: Syntax tweak to not upset syntax highlighting
Kim Alvefur <zash@zash.se>
parents: 11446
diff changeset
214 for _, v in ipairs(schema["enum"]) do
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 if v == data then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 return false
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
11448
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
222 if schema.type then
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
223 if not simple_validate(schema.type, data) then
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
224 return false
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
225 end
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
226
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
227 local validator = type_validators[schema.type]
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
228 if validator then
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
229 return validator(schema, data)
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
230 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 end
11448
1d84b54ba0d7 util.jsonschema: Restructure "type" keyword handling
Kim Alvefur <zash@zash.se>
parents: 11447
diff changeset
232 return true
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 end
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
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
236 type_validators.table = 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
237 if data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 if schema.maxItems and #data > schema.maxItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 if schema.minItems and #data < schema.minItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 if schema.properties then
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
256 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
257 for k, v in pairs(data) do
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
258 if schema.propertyNames and not validate(schema.propertyNames, k) then
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
259 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
260 end
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
261 local s = schema.properties[k as string] or additional
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
262 if not validate(s, v) then
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
263 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 end
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
266 elseif schema.additionalProperties then
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 for k, v in pairs(data) do
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
268 if schema.propertyNames and not validate(schema.propertyNames, k) then
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
269 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
270 end
11445
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
271 if not validate(schema.additionalProperties, v) then
c73744fa3bdf util.jsonschema: Restructure handling of "properties" and "additionalProperties"
Kim Alvefur <zash@zash.se>
parents: 11444
diff changeset
272 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 if schema.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 -- 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
279 local values : { any : boolean } = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 if values[v] then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 end
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
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
288 local p = 0
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
289 if schema.prefixItems then
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
290 for i, s in ipairs(schema.prefixItems) do
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
291 if validate(s, data[i]) then
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
292 p = i
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
293 else
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
294 return false
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
295 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
296 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
297 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
298
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
299 if schema.items then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
300 for i = p+1, #data do
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
301 if not validate(schema.items, data[i]) then
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
302 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
307 if schema.contains then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
308 local found = false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
309 for i = 1, #data do
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
310 if validate(schema.contains, data[i]) then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
311 found = true
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
312 break
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
313 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
314 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
315 if not found then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
316 return false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
317 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
318 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
319
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 return true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 type_validators.object = function (schema : schema_t, data : any) : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 if data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 for k in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 if not k is string then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 return false
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
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 return type_validators.table(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 type_validators.array = function (schema : schema_t, data : any) : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 if data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 -- 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
342 for i in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 if not i is number then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 return false
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 return type_validators.table(schema, data)
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 return false
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
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
353 json_schema_object.validate = validate;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
354
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
355 return json_schema_object;