-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_dc_schema.py
574 lines (487 loc) · 15.4 KB
/
test_dc_schema.py
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
from __future__ import annotations
import datetime
import dataclasses
import typing as t
import enum
from jsonschema.validators import Draft202012Validator
from dc_schema import (
get_schema,
SchemaAnnotation,
)
@dataclasses.dataclass
class DcPrimitives:
b: bool
i: int
f: float
s: str
def test_get_schema_primitives():
schema = get_schema(DcPrimitives)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcPrimitives",
"properties": {
"b": {"type": "boolean"},
"i": {"type": "integer"},
"f": {"type": "number"},
"s": {"type": "string"},
},
"required": ["b", "i", "f", "s"],
}
@dataclasses.dataclass
class DcOptional:
a: int = 42
b: int = dataclasses.field(default=42)
c: int = dataclasses.field(default_factory=lambda: 42)
d: str = "foo"
e: bool = False
f: None = None
g: float = 1.1
h: tuple[int, float] = (1, 1.1)
def test_get_schema_optional_fields():
"""optional field === field with a default (!== t.Optional)"""
schema = get_schema(DcOptional)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcOptional",
"properties": {
"a": {"type": "integer", "default": 42},
"b": {"type": "integer", "default": 42},
"c": {"type": "integer"},
"d": {"type": "string", "default": "foo"},
"e": {"type": "boolean", "default": False},
"f": {"type": "null", "default": None},
"g": {"type": "number", "default": 1.1},
"h": {
"type": "array",
"prefixItems": [{"type": "integer"}, {"type": "number"}],
"minItems": 2,
"maxItems": 2,
"default": [1, 1.1],
},
},
}
@dataclasses.dataclass
class DcUnion:
a: t.Union[int, str]
def test_get_schema_union():
schema = get_schema(DcUnion)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcUnion",
"properties": {"a": {"anyOf": [{"type": "integer"}, {"type": "string"}]}},
"required": ["a"],
}
@dataclasses.dataclass
class DcNone:
a: None
b: t.Optional[int]
c: t.Union[None, int]
def test_get_schema_nullable():
schema = get_schema(DcNone)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcNone",
"properties": {
"a": {"type": "null"},
"b": {"anyOf": [{"type": "integer"}, {"type": "null"}]},
"c": {"anyOf": [{"type": "null"}, {"type": "integer"}]},
},
"required": ["a", "b", "c"],
}
@dataclasses.dataclass
class DcDict:
a: dict
b: dict[str, int]
def test_get_schema_dict():
schema = get_schema(DcDict)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcDict",
"properties": {
"a": {"type": "object"},
"b": {"type": "object", "additionalProperties": {"type": "integer"}},
},
"required": ["a", "b"],
}
@dataclasses.dataclass
class DcList:
a: list
b: list[bool]
def test_get_schema_list():
schema = get_schema(DcList)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcList",
"properties": {
"a": {"type": "array"},
"b": {"type": "array", "items": {"type": "boolean"}},
},
"required": ["a", "b"],
}
@dataclasses.dataclass
class DcTuple:
a: tuple
b: tuple[int, ...]
c: tuple[int, bool, str]
def test_get_schema_tuple():
schema = get_schema(DcTuple)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcTuple",
"properties": {
"a": {"type": "array"},
"b": {"type": "array", "items": {"type": "integer"}},
"c": {
"type": "array",
"prefixItems": [
{"type": "integer"},
{"type": "boolean"},
{"type": "string"},
],
"minItems": 3,
"maxItems": 3,
},
},
"required": ["a", "b", "c"],
}
@dataclasses.dataclass
class DcRefsChild:
c: str
@dataclasses.dataclass
class DcRefs:
a: DcRefsChild
b: list[DcRefsChild]
def test_get_schema_refs():
schema = get_schema(DcRefs)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcRefs",
"properties": {
"a": {"allOf": [{"$ref": "#/$defs/DcRefsChild"}]},
"b": {
"type": "array",
"items": {"allOf": [{"$ref": "#/$defs/DcRefsChild"}]},
},
},
"required": ["a", "b"],
"$defs": {
"DcRefsChild": {
"type": "object",
"title": "DcRefsChild",
"properties": {"c": {"type": "string"}},
"required": ["c"],
}
},
}
@dataclasses.dataclass
class DcRefsSelf:
a: str
b: t.Optional[DcRefsSelf]
c: list[DcRefsSelf]
def test_get_schema_self_refs():
schema = get_schema(DcRefsSelf)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcRefsSelf",
"properties": {
"a": {"type": "string"},
"b": {"anyOf": [{"allOf": [{"$ref": "#"}]}, {"type": "null"}]},
"c": {"type": "array", "items": {"allOf": [{"$ref": "#"}]}},
},
"required": ["a", "b", "c"],
}
@dataclasses.dataclass
class DcLiteral:
a: t.Literal[1, "two", 3, None]
b: t.Literal[42, 43] = 42
def test_get_schema_literal():
schema = get_schema(DcLiteral)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcLiteral",
"properties": {
"a": {"enum": [1, "two", 3, None]},
"b": {"enum": [42, 43], "default": 42},
},
"required": ["a"],
}
class MyEnum(enum.Enum):
a = enum.auto()
b = enum.auto()
@dataclasses.dataclass
class DcEnum:
a: MyEnum
b: MyEnum = MyEnum.a
def test_get_schema_enum():
schema = get_schema(DcEnum)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcEnum",
"properties": {
"a": {"allOf": [{"$ref": "#/$defs/MyEnum"}]},
"b": {"allOf": [{"$ref": "#/$defs/MyEnum"}], "default": 1},
},
"required": ["a"],
"$defs": {"MyEnum": {"title": "MyEnum", "enum": [1, 2]}},
}
@dataclasses.dataclass
class DcSet:
a: set
b: set[int]
def test_get_schema_set():
schema = get_schema(DcSet)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcSet",
"properties": {
"a": {"type": "array", "uniqueItems": True},
"b": {"type": "array", "items": {"type": "integer"}, "uniqueItems": True},
},
"required": ["a", "b"],
}
@dataclasses.dataclass
class DcStrAnnotated:
a: t.Annotated[str, SchemaAnnotation(min_length=3, max_length=5)]
b: t.Annotated[
str, SchemaAnnotation(format="date", pattern=r"^\d.*")
] = "2000-01-01"
def test_get_schema_str_annotation():
schema = get_schema(DcStrAnnotated)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcStrAnnotated",
"properties": {
"a": {"type": "string", "minLength": 3, "maxLength": 5},
"b": {
"type": "string",
"default": "2000-01-01",
"pattern": "^\\d.*",
"format": "date",
},
},
"required": ["a"],
}
@dataclasses.dataclass
class DcNumberAnnotated:
a: t.Annotated[int, SchemaAnnotation(minimum=1, exclusive_maximum=11)]
b: list[t.Annotated[int, SchemaAnnotation(minimum=0)]]
c: t.Optional[t.Annotated[int, SchemaAnnotation(minimum=0)]]
d: t.Annotated[
float, SchemaAnnotation(maximum=12, exclusive_minimum=17, multiple_of=5)
] = 33.1
def test_get_schema_number_annotation():
schema = get_schema(DcNumberAnnotated)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcNumberAnnotated",
"properties": {
"a": {"type": "integer", "minimum": 1, "exclusiveMaximum": 11},
"b": {"type": "array", "items": {"type": "integer", "minimum": 0}},
"c": {"anyOf": [{"type": "integer", "minimum": 0}, {"type": "null"}]},
"d": {
"type": "number",
"default": 33.1,
"maximum": 12,
"exclusiveMinimum": 17,
"multipleOf": 5,
},
},
"required": ["a", "b", "c"],
}
@dataclasses.dataclass
class DcDateTime:
a: datetime.datetime
b: datetime.date
def test_get_schema_date_time():
schema = get_schema(DcDateTime)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcDateTime",
"properties": {
"a": {"type": "string", "format": "date-time"},
"b": {"type": "string", "format": "date"},
},
"required": ["a", "b"],
}
@dataclasses.dataclass
class DcAnnotatedBook:
title: t.Annotated[str, SchemaAnnotation(title="Title")]
class DcAnnotatedAuthorHobby(enum.Enum):
CHESS = "chess"
SOCCER = "soccer"
@dataclasses.dataclass
class DcAnnotatedAuthor:
name: t.Annotated[
str,
SchemaAnnotation(
description="the name of the author", examples=["paul", "alice"]
),
]
books: t.Annotated[
list[DcAnnotatedBook],
SchemaAnnotation(description="all the books the author has written"),
]
hobby: t.Annotated[DcAnnotatedAuthorHobby, SchemaAnnotation(deprecated=True)]
age: t.Annotated[
t.Union[int, float], SchemaAnnotation(description="age in years")
] = 42
def test_get_schema_annotation():
schema = get_schema(DcAnnotatedAuthor)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcAnnotatedAuthor",
"properties": {
"name": {
"type": "string",
"description": "the name of the author",
"examples": ["paul", "alice"],
},
"books": {
"type": "array",
"items": {"allOf": [{"$ref": "#/$defs/DcAnnotatedBook"}]},
"description": "all the books the author has written",
},
"hobby": {
"allOf": [{"$ref": "#/$defs/DcAnnotatedAuthorHobby"}],
"deprecated": True,
},
"age": {
"anyOf": [{"type": "integer"}, {"type": "number"}],
"default": 42,
"description": "age in years",
},
},
"required": ["name", "books", "hobby"],
"$defs": {
"DcAnnotatedBook": {
"type": "object",
"title": "DcAnnotatedBook",
"properties": {"title": {"type": "string", "title": "Title"}},
"required": ["title"],
},
"DcAnnotatedAuthorHobby": {
"title": "DcAnnotatedAuthorHobby",
"enum": ["chess", "soccer"],
},
},
}
@dataclasses.dataclass
class DcSchemaConfigChild:
a: int
class SchemaConfig:
annotation = SchemaAnnotation(title="a child")
@dataclasses.dataclass
class DcSchemaConfig:
a: str
child_1: DcSchemaConfigChild
child_2: t.Annotated[DcSchemaConfigChild, SchemaAnnotation(title="2nd child")]
friend: t.Annotated[DcSchemaConfig, SchemaAnnotation(title="a friend")]
class SchemaConfig:
annotation = SchemaAnnotation(title="root model")
def test_get_schema_config():
schema = get_schema(DcSchemaConfig)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "root model",
"properties": {
"a": {"type": "string"},
"child_1": {"allOf": [{"$ref": "#/$defs/DcSchemaConfigChild"}]},
"child_2": {
"allOf": [{"$ref": "#/$defs/DcSchemaConfigChild"}],
"title": "2nd child",
},
"friend": {"allOf": [{"$ref": "#"}], "title": "a friend"},
},
"required": ["a", "child_1", "child_2", "friend"],
"$defs": {
"DcSchemaConfigChild": {
"type": "object",
"title": "a child",
"properties": {"a": {"type": "integer"}},
"required": ["a"],
}
},
}
@dataclasses.dataclass
class DcListAnnotation:
a: t.Annotated[
list[int], SchemaAnnotation(min_items=3, max_items=5, unique_items=True)
]
b: t.Annotated[tuple[float, ...], SchemaAnnotation(min_items=3, max_items=10)] = ()
def test_get_schema_list_annotation():
schema = get_schema(DcListAnnotation)
print(schema)
Draft202012Validator.check_schema(schema)
assert schema == {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "DcListAnnotation",
"properties": {
"a": {
"type": "array",
"items": {"type": "integer"},
"minItems": 3,
"maxItems": 5,
"uniqueItems": True,
},
"b": {
"type": "array",
"items": {"type": "number"},
"default": [],
"minItems": 3,
"maxItems": 10,
},
},
"required": ["a"],
}