forked from pydantic/pydantic
-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema_mapping.py
executable file
·506 lines (493 loc) · 11.6 KB
/
schema_mapping.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
#!/usr/bin/env python3
"""
Build a table of Python / Pydantic to JSON Schema mappings.
Done like this rather than as a raw rst table to make future edits easier.
Please edit this file directly not .tmp_schema_mappings.html
"""
import json
import re
from pathlib import Path
table = [
[
'bool',
'boolean',
'',
'JSON Schema Core',
''
],
[
'str',
'string',
'',
'JSON Schema Core',
''
],
[
'float',
'number',
'',
'JSON Schema Core',
''
],
[
'int',
'integer',
'',
'JSON Schema Validation',
''
],
[
'dict',
'object',
'',
'JSON Schema Core',
''
],
[
'list',
'array',
{'items': {}},
'JSON Schema Core',
''
],
[
'tuple',
'array',
{'items': {}},
'JSON Schema Core',
''
],
[
'set',
'array',
{'items': {}, 'uniqueItems': True},
'JSON Schema Validation',
''
],
[
'List[str]',
'array',
{'items': {'type': 'string'}},
'JSON Schema Validation',
'And equivalently for any other sub type, e.g. `List[int]`.'
],
[
'Tuple[str, int]',
'array',
{'items': [{'type': 'string'}, {'type': 'integer'}]},
'JSON Schema Validation',
(
'And equivalently for any other set of subtypes. Note: If using schemas for OpenAPI, '
'you shouldn\'t use this declaration, as it would not be valid in OpenAPI (although it is '
'valid in JSON Schema).'
)
],
[
'Dict[str, int]',
'object',
{'additionalProperties': {'type': 'integer'}},
'JSON Schema Validation',
(
'And equivalently for any other subfields for dicts. Have in mind that although you can use other types as '
'keys for dicts with Pydantic, only strings are valid keys for JSON, and so, only str is valid as '
'JSON Schema key types.'
)
],
[
'Union[str, int]',
'anyOf',
{'anyOf': [{'type': 'string'}, {'type': 'integer'}]},
'JSON Schema Validation',
'And equivalently for any other subfields for unions.'
],
[
'Enum',
'enum',
'{"enum": [...]}',
'JSON Schema Validation',
'All the literal values in the enum are included in the definition.'
],
[
'SecretStr',
'string',
{'writeOnly': True},
'JSON Schema Validation',
''
],
[
'SecretBytes',
'string',
{'writeOnly': True},
'JSON Schema Validation',
''
],
[
'EmailStr',
'string',
{'format': 'email'},
'JSON Schema Validation',
''
],
[
'NameEmail',
'string',
{'format': 'name-email'},
'Pydantic standard "format" extension',
''
],
[
'AnyUrl',
'string',
{'format': 'uri'},
'JSON Schema Validation',
''
],
[
'Pattern',
'string',
{'format': 'regex'},
'JSON Schema Validation',
''
],
[
'bytes',
'string',
{'format': 'binary'},
'OpenAPI',
''
],
[
'Decimal',
'number',
'',
'JSON Schema Core',
''
],
[
'UUID1',
'string',
{'format': 'uuid1'},
'Pydantic standard "format" extension',
''
],
[
'UUID3',
'string',
{'format': 'uuid3'},
'Pydantic standard "format" extension',
''
],
[
'UUID4',
'string',
{'format': 'uuid4'},
'Pydantic standard "format" extension',
''
],
[
'UUID5',
'string',
{'format': 'uuid5'},
'Pydantic standard "format" extension',
''
],
[
'UUID',
'string',
{'format': 'uuid'},
'Pydantic standard "format" extension',
'Suggested in OpenAPI.'
],
[
'FilePath',
'string',
{'format': 'file-path'},
'Pydantic standard "format" extension',
''
],
[
'DirectoryPath',
'string',
{'format': 'directory-path'},
'Pydantic standard "format" extension',
''
],
[
'Path',
'string',
{'format': 'path'},
'Pydantic standard "format" extension',
''
],
[
'datetime',
'string',
{'format': 'date-time'},
'JSON Schema Validation',
''
],
[
'date',
'string',
{'format': 'date'},
'JSON Schema Validation',
''
],
[
'time',
'string',
{'format': 'time'},
'JSON Schema Validation',
''
],
[
'timedelta',
'number',
{'format': 'time-delta'},
'Difference in seconds (a `float`), with Pydantic standard "format" extension',
'Suggested in JSON Schema repository\'s issues by maintainer.'
],
[
'Json',
'string',
{'format': 'json-string'},
'Pydantic standard "format" extension',
''
],
[
'IPv4Address',
'string',
{'format': 'ipv4'},
'JSON Schema Validation',
''
],
[
'IPv6Address',
'string',
{'format': 'ipv6'},
'JSON Schema Validation',
''
],
[
'IPvAnyAddress',
'string',
{'format': 'ipvanyaddress'},
'Pydantic standard "format" extension',
'IPv4 or IPv6 address as used in `ipaddress` module',
],
[
'IPv4Interface',
'string',
{'format': 'ipv4interface'},
'Pydantic standard "format" extension',
'IPv4 interface as used in `ipaddress` module',
],
[
'IPv6Interface',
'string',
{'format': 'ipv6interface'},
'Pydantic standard "format" extension',
'IPv6 interface as used in `ipaddress` module',
],
[
'IPvAnyInterface',
'string',
{'format': 'ipvanyinterface'},
'Pydantic standard "format" extension',
'IPv4 or IPv6 interface as used in `ipaddress` module',
],
[
'IPv4Network',
'string',
{'format': 'ipv4network'},
'Pydantic standard "format" extension',
'IPv4 network as used in `ipaddress` module',
],
[
'IPv6Network',
'string',
{'format': 'ipv6network'},
'Pydantic standard "format" extension',
'IPv6 network as used in `ipaddress` module',
],
[
'IPvAnyNetwork',
'string',
{'format': 'ipvanynetwork'},
'Pydantic standard "format" extension',
'IPv4 or IPv6 network as used in `ipaddress` module',
],
[
'StrictBool',
'boolean',
'',
'JSON Schema Core',
''
],
[
'StrictStr',
'string',
'',
'JSON Schema Core',
''
],
[
'ConstrainedStr',
'string',
'',
'JSON Schema Core',
(
'If the type has values declared for the constraints, they are included as validations. '
'See the mapping for `constr` below.'
)
],
[
'constr(regex=\'^text$\', min_length=2, max_length=10)',
'string',
{'pattern': '^text$', 'minLength': 2, 'maxLength': 10},
'JSON Schema Validation',
'Any argument not passed to the function (not defined) will not be included in the schema.'
],
[
'ConstrainedInt',
'integer',
'',
'JSON Schema Core',
(
'If the type has values declared for the constraints, they are included as validations. '
'See the mapping for `conint` below.'
)
],
[
'conint(gt=1, ge=2, lt=6, le=5, multiple_of=2)',
'integer',
{'maximum': 5, 'exclusiveMaximum': 6, 'minimum': 2, 'exclusiveMinimum': 1, 'multipleOf': 2},
'',
'Any argument not passed to the function (not defined) will not be included in the schema.'
],
[
'PositiveInt',
'integer',
{'exclusiveMinimum': 0},
'JSON Schema Validation',
''
],
[
'NegativeInt',
'integer',
{'exclusiveMaximum': 0},
'JSON Schema Validation',
''
],
[
'ConstrainedFloat',
'number',
'',
'JSON Schema Core',
(
'If the type has values declared for the constraints, they are included as validations. '
'See the mapping for `confloat` below.'
)
],
[
'confloat(gt=1, ge=2, lt=6, le=5, multiple_of=2)',
'number',
{'maximum': 5, 'exclusiveMaximum': 6, 'minimum': 2, 'exclusiveMinimum': 1, 'multipleOf': 2},
'JSON Schema Validation',
'Any argument not passed to the function (not defined) will not be included in the schema.'
],
[
'PositiveFloat',
'number',
{'exclusiveMinimum': 0},
'JSON Schema Validation',
''
],
[
'NegativeFloat',
'number',
{'exclusiveMaximum': 0},
'JSON Schema Validation',
''
],
[
'ConstrainedDecimal',
'number',
'',
'JSON Schema Core',
(
'If the type has values declared for the constraints, they are included as validations. '
'See the mapping for `condecimal` below.'
)
],
[
'condecimal(gt=1, ge=2, lt=6, le=5, multiple_of=2)',
'number',
{'maximum': 5, 'exclusiveMaximum': 6, 'minimum': 2, 'exclusiveMinimum': 1, 'multipleOf': 2},
'JSON Schema Validation',
'Any argument not passed to the function (not defined) will not be included in the schema.'
],
[
'BaseModel',
'object',
'',
'JSON Schema Core',
'All the properties defined will be defined with standard JSON Schema, including submodels.'
],
[
'Color',
'string',
{'format': 'color'},
'Pydantic standard "format" extension',
'',
],
]
headings = [
'Python type',
'JSON Schema Type',
'Additional JSON Schema',
'Defined in',
]
def md2html(s):
return re.sub(r'`(.+?)`', r'<code>\1</code>', s)
def build_schema_mappings():
rows = []
for py_type, json_type, additional, defined_in, notes in table:
if additional and not isinstance(additional, str):
additional = json.dumps(additional)
cols = [
f'<code>{py_type}</code>',
f'<code>{json_type}</code>',
f'<code>{additional}</code>' if additional else '',
md2html(defined_in)
]
rows.append('\n'.join(f' <td>\n {c}\n </td>' for c in cols))
if notes:
rows.append(
f' <td colspan=4 style="border-top: none; padding-top: 0">\n'
f' <em>{md2html(notes)}</em>\n'
f' </td>'
)
heading = '\n'.join(f' <th>{h}</th>' for h in headings)
body = '\n</tr>\n<tr>\n'.join(rows)
text = f"""\
<!--
Generated from docs/build/schema_mapping.py, DO NOT EDIT THIS FILE DIRECTLY.
Instead edit docs/build/schema_mapping.py and run `make docs`.
-->
<table style="width:100%">
<thead>
<tr>
{heading}
</tr>
</thead>
<tbody>
<tr>
{body}
</tr>
</tbody>
</table>
"""
(Path(__file__).parent / '..' / '.tmp_schema_mappings.html').write_text(text)
if __name__ == '__main__':
build_schema_mappings()