Annotate

teal-src/util/jsonschema.tl @ 12884:f5a75aaa8a25

util.poll: Include unistd.h only for epoll This defines close(), which is only used with epoll, hence we don't need to include it when building in poll or select mode.
author Kim Alvefur <zash@zash.se>
date Fri, 10 Feb 2023 00:37:05 +0100
parent 12782:8815d3090928
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
12782
8815d3090928 util.mathcompat: Module to ease reuse of math.type()
Kim Alvefur <zash@zash.se>
parents: 12579
diff changeset
11 if not math.type then require "util.mathcompat" end
8815d3090928 util.mathcompat: Module to ease reuse of math.type()
Kim Alvefur <zash@zash.se>
parents: 12579
diff changeset
12
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
13 local json = require"util.json"
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
14 local null = json.null;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
15
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
16 local pointer = require "util.jsonpointer"
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
17
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
18 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
19
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
20 -- 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
21 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
22
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
23 local record json_schema_object
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
24 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
25 type schema_object = json_schema_object
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
26
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
27 type : json_type_name | { json_type_name }
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 enum : { any }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 const : any
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 allOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 anyOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 oneOf : { schema_t }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 ["not"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 ["if"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 ["then"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 ["else"] : schema_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
40 ["$ref"] : string
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
41
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 -- numbers
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 multipleOf : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 maximum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 exclusiveMaximum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 minimum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 exclusiveMinimum : number
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 -- strings
11459
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
50 maxLength : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
51 minLength : integer
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
52 pattern : string -- NYI
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 format : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 -- arrays
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
56 prefixItems : { schema_t }
11440
d5288c99bb5a util.jsonschema: Correct "items" keyword
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
57 items : schema_t
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
58 contains : schema_t
11459
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
59 maxItems : integer
86904555bffc teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents: 11449
diff changeset
60 minItems : integer
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 uniqueItems : boolean
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
62 maxContains : integer -- NYI
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
63 minContains : integer -- NYI
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 -- objects
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
66 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
67 maxProperties : integer -- NYI
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
68 minProperties : integer -- NYI
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 required : { string }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 dependentRequired : { string : { string } }
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 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
72 patternProperties: schema_t -- NYI
11446
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
73 propertyNames : schema_t
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 -- xml
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 record xml_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 name : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 namespace : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 prefix : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 attribute : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 wrapped : boolean
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 -- nonstantard, maybe in the future
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 text : boolean
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11434
diff changeset
85 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
86 x_single_attribute : string
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
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 xml : xml_t
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 -- descriptive
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 title : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 description : string
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 deprecated : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 readOnly : boolean
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 writeOnly : boolean
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
97
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
98 -- methods
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
99 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
100 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 -- TODO validator function per schema property
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
104 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
105 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
106 return true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
107 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
108 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
109 elseif schema == "array" and data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 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
111 elseif schema == "integer" then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return math.type(data) == schema
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
113 elseif schema == "null" then
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
114 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
115 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
116 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
117 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
118 return true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
119 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
120 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
121 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 else
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 return type(data) == schema
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
127 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
128
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
129 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
130 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
131 return schema
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
132 else
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
133 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
134 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
135 end
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 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
138
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
139 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
140 root = schema
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
141 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
142
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
143 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
144 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
145 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
146 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
147 return false;
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 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
150 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
151
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
152 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
153 return false;
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
154 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
155
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
156 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
157 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
158 -- 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
159 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
160 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
161 return false
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 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
165 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
166
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
167 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
168 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
169 -- 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
170 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
171 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
172 return false
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 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
176 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
177
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
178 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
179 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
180 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
181 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
182 -- 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
183 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
184 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
185 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
186 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
187 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
188 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
189 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
190 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
191
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 -- 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
193 -- TODO use utf8.len?
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 if data is string then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 if schema.maxLength and #data > schema.maxLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 if schema.minLength and #data < schema.minLength then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
202
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
203 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
204 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
205 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
206 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
207
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
208 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
209 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
210 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
211
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
212 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
213 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
214 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
216 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
217 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
218 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
219
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
220 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
221 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
222 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
223 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
224
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
225 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
226 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
227 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
228 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
229 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
230 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
233 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
234 local valid = 0
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
235 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
236 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
237 valid = valid + 1
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
238 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
239 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
240 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
241 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
242 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
245 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
246 local match = false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
247 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
248 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
249 match = true
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
250 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
251 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
252 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
253 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
254 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
255 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
258 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
259 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
260 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
261 end
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
264 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
265 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
266 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
267 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
268 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
269 else
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
270 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
271 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
272 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
273 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
274 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
275
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
276 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
277 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 if data is table then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 if schema.maxItems and #data > schema.maxItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 if schema.minItems and #data < schema.minItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 return false
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 if schema.required then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 for _, k in ipairs(schema.required) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 if data[k] == nil then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 return false
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
298 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
299 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
300 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
301 return false
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
304 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
305
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
306 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
307 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
308 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
309 return false
58c534bac798 util.jsonschema: Implement "propertyNames"
Kim Alvefur <zash@zash.se>
parents: 11445
diff changeset
310 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
311 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
312 end
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
313
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
314 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
315 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
316 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
317 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
318 return false
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
319 end
11434
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 if schema.uniqueItems then
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 -- 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
326 local values : { any : boolean } = {}
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 for _, v in pairs(data) do
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 if values[v] 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 values[v] = true
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
335 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
336 if schema.prefixItems ~= nil then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
337 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
338 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
339 break
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
340 elseif validate(s, data[i], root) then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
341 p = i
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
342 else
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
343 return false
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 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
346 end
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
347
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
348 if schema.items ~= nil then
11443
a526abef61e6 util.jsonschema: Implement the "prefixItems" keyword
Kim Alvefur <zash@zash.se>
parents: 11442
diff changeset
349 for i = p+1, #data do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
350 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
351 return false
11434
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 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
356 if schema.contains ~= nil then
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
357 local found = false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
358 for i = 1, #data do
12132
4ff0d33dfb2b util.jsonschema: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11460
diff changeset
359 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
360 found = true
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
361 break
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
362 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
363 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
364 if not found then
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
365 return false
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
366 end
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
367 end
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
368 end
11442
95f0d77175ca util.jsonschema: Implement the "contains" keyword
Kim Alvefur <zash@zash.se>
parents: 11441
diff changeset
369
12579
ca6a43fe0231 util.jsonschema: Fix validation to not assume presence of "type" field
Kim Alvefur <zash@zash.se>
parents: 12132
diff changeset
370 return true;
11434
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 end
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372
66d4067bdfb2 util.jsonschema: Library for JSON Schema validation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373
11460
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
374 json_schema_object.validate = validate;
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
375
a8b4e04bc044 util.jsonschema: Rename types for improved readability
Kim Alvefur <zash@zash.se>
parents: 11459
diff changeset
376 return json_schema_object;