-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.v
268 lines (253 loc) · 5.86 KB
/
schema.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module openapi
import x.json2 { Any }
import json
import regex
pub struct Schema {
pub mut:
title string
multiple_of f64
maximum f64
minimum f64
exclusive_maximum bool
exclusive_minimum bool
max_length u64
min_length u64
pattern string
max_items u64
min_items u64
unique_items bool
max_properties u64
min_properties u64
required []string
enum_values []Any
type_schema string
all_of ObjectRef<Schema>
one_of ObjectRef<Schema>
any_of ObjectRef<Schema>
not ObjectRef<Schema>
items ObjectRef<Schema>
properties map[string]ObjectRef<Schema>
additional_properties ObjectRef<Schema>
description string
format string
default_value Any
nullable bool
discriminator Discriminator
read_only bool
write_only bool
xml XML
external_docs ExternalDocumentation
example Any
deprecated bool
}
pub fn (mut schema Schema) from_json(json Any) ? {
object := json.as_map()
for key, value in object {
match key {
'title' {
schema.title = value.str()
}
'pattern' {
schema.pattern = value.str()
}
'type' {
schema.type_schema = value.str()
}
'description' {
schema.description = value.str()
}
'format' {
schema.format = value.str()
}
'multipleOf' {
schema.multiple_of = value.f64()
}
'maximum' {
schema.maximum = value.f64()
}
'minimum' {
schema.minimum = value.f64()
}
'maxLength' {
schema.max_length = value.u64()
}
'minLength' {
schema.min_length = value.u64()
}
'maxItems' {
schema.max_items = value.u64()
}
'minItems' {
schema.min_items = value.u64()
}
'maxProperties' {
schema.max_properties = value.u64()
}
'minProperties' {
schema.min_properties = value.u64()
}
'exclusiveMinimum' {
schema.exclusive_minimum = value.bool()
}
'exclusiveMaximum' {
schema.exclusive_maximum = value.bool()
}
'UniqueItems' {
schema.unique_items = value.bool()
}
'nullable' {
schema.nullable = value.bool()
}
'readOnly' {
schema.read_only = value.bool()
}
'writeOnly' {
schema.write_only = value.bool()
}
'deprecated' {
schema.deprecated = value.bool()
}
'default' {
schema.default_value = value
}
'example' {
schema.example = value
}
'discriminator' {
schema.discriminator = decode<Discriminator>(value.json_str())?
}
'xml' {
schema.xml = decode<XML>(value.json_str())?
}
'externalDocs' {
schema.external_docs = decode<ExternalDocumentation>(value.json_str())?
}
'allOf' {
schema.all_of = from_json<Schema>(value)?
}
'oneOf' {
schema.one_of = from_json<Schema>(value)?
}
'anyOf' {
schema.any_of = from_json<Schema>(value)?
}
'not' {
schema.not = from_json<Schema>(value)?
}
'items' {
schema.items = from_json<Schema>(value)?
}
'additionalProperties' {
schema.additional_properties = from_json<Schema>(value)?
}
'required' {
schema.required = decode_array_string(value.json_str())?
}
'enum' {
schema.enum_values = decode_array_any(value.json_str())?
}
'properties' {
schema.properties = decode_map_sumtype<Schema>(value.json_str(), fake_predicat)?
}
else {}
}
schema.validate(object)?
}
}
fn (mut schema Schema) validate(object map[string]Any) ? {
if 'readOnly' in object && 'writeOnly' in object {
return error('Failed Schema decoding: Schema cannot be writeOnly and readOnly')
}
mut format_values := []string{}
match schema.type_schema {
'' {}
'boolean' {}
'object' {}
'array' {
if 'items' !in object {
println(schema.items)
return error('Failed Schema decoding: "items" must be non-null if "type" is "array"')
}
}
'integer' {
format_values = ['int32', 'int64']
}
'number' {
format_values = ['float', 'double']
}
'string' {
format_values = ['byte', 'binary', 'date', 'date-time', 'password', 'iri',
'iri-reference', 'uri-template', 'idn-email', 'idn-hostname', 'json-pointer',
'relative-json-pointer', 'regex', 'time', 'duration', 'uuid', 'email', 'hostname',
'ipv4', 'ipv6', 'uri', 'uri-reference']
if schema.pattern != '' {
regex.regex_opt(schema.pattern) or {
return error('Failed Schema decoding: invalid pattern regex $schema.pattern')
}
}
}
'null' {
if !schema.nullable {
return error('Failed Schema decoding: "nullable" must be true to have a null "type"')
}
}
else {
return error('Failed Schema decoding: unsupported type $schema.type_schema')
}
}
if schema.format != '' && format_values.len != 0 && schema.format !in format_values {
return error('Failed Schema decoding: unsupported format value $schema.format')
}
}
// ---------------------------------------- //
pub struct Discriminator {
pub mut:
property_name string
mapping map[string]string
}
pub fn (mut discriminator Discriminator) from_json(json Any) ? {
object := json.as_map()
check_required<Discriminator>(object, 'propertyName')?
for key, value in object {
match key {
'propertyName' {
discriminator.property_name = value.str()
}
'mapping' {
discriminator.mapping = decode_map_string(value.json_str())?
}
else {}
}
}
}
// ---------------------------------------- //
pub struct XML {
pub mut:
name string
namespace string
prefix string
attribute bool
wrapped bool
}
pub fn (mut xml XML) from_json(json Any) ? {
for key, value in json.as_map() {
match key {
'name' {
xml.name = value.str()
}
'namespace' {
xml.namespace = value.str()
}
'prefix' {
xml.prefix = value.str()
}
'attribute' {
xml.attribute = value.bool()
}
'wrapped' {
xml.wrapped = value.bool()
}
else {}
}
}
}