mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
94706bc834
* Update schema tests to conform to JSON Schema spec * Add JSON Schema tests for all supported types including datetime and all supported Pydantic.types * Add JSON Schema conforming schema sub module * Update BaseModel to use schema module for JSON Schema generation and update/simplify internal Schema methods * Remove Schema code from Field class, replaced with JSON Schema module * Add submodules to test model name generation for JSON Schemas * Refactor/rewrite schema module to generate definions and refs * Update and augment JSON Schema tests to include definitions and refs and generation of a single JSON Schema with definitions from multiple (unrelated) models * Add ref_prefix functionality to JSON Schema generation functions * Test custom ref_prefix in JSON Schema generation * Remove un-used BaseModel method, now refactored to schema module * Update formating of test_schema * Fix long lines in test_schema * Fix imported but unused in fields * Fix imported but unused in main.py * Ignore imported but unused for testing modulec * Refactor schema module for complexity * Add conflicting name model to raise coverage * Add conflicting model to test other flow and raise coverage * Ignore complexity as destructuring more would make it more complex and more difficult to understand, similar to .fields.validate * Fix import sorting * Update formatting with black, with CI settings * Fix test for schemas with email validation * Check if field is class before checking if is subclass * Improve schema error when using unsuported types * Add additional tests for corner cases, raise coverage to 100% * Rename BaseModel.schema_json to schema_str (EAFP Python style) * Add more tests to utils.display_as_type to increase the coverage for enums * Remove unused catched error in schema tests * Fix formatting with black * Update docs schema example * Add schema examples for top-level schema with multiple models * Update docs, section Schema, with new JSON Schema generation details * Update docs, history, with new features * Update fields, remove unnecessary schema code for enums * Update docs, fix links and typos in Schema section * Trigger CI, as Python 3.7-dev seems to have random CI errors * Revert Model.schema_str to Model.schema_json as requested * Remove unnecessary assert in schema module as requested * Remove annotations in internal functions, as requested * Refactor get_flat_models_from_fields and reuse * Use set short assignment syntax in schema module * Remove unwanted assertion * Make get_long_model_name a single line f-string * Update model_name_map, add docstring and remove first return value * Simplify dict operation in get_model_name_map as requested * Make more concise model_name_map computation * Remove bool from field check in schema as is subclass of int * Make ref_prefix default to None and use global default * Fix formatting for schema.py * Refactor field_singleton_schema to use data structures * Move main functions to top of schema, and add docstrings for them * Implement __all__, move and order parts of schema * Remove schema testing sub-package code as requested * Generate schema testing subpackage in code * Update schema tests with several related fields to use parametrized pytest * Fix formatting and imports I missed after rebase * Fix new formatting errors from CI * Re-trigger Travis CI, Python 3.7-dev random error again, no re-run click in Travis for non owners * Trigger annotation error with non-forward references * Add docstrings for submodel schema * tweaks and rewrite schema mapping table in python * support complex defaults * use str not int as dict keys * Fix links to JSON Schema and OpenAPI
386 lines
8.7 KiB
Python
Executable File
386 lines
8.7 KiB
Python
Executable File
#!/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.rst
|
|
"""
|
|
|
|
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',
|
|
'',
|
|
'JSON Schema Core',
|
|
''
|
|
],
|
|
[
|
|
'tuple',
|
|
'array',
|
|
'',
|
|
'JSON Schema Core',
|
|
''
|
|
],
|
|
[
|
|
'set',
|
|
'array',
|
|
'{"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.'
|
|
],
|
|
[
|
|
'EmailStr',
|
|
'string',
|
|
'{"format": "email"}',
|
|
'JSON Schema Validation',
|
|
''
|
|
],
|
|
[
|
|
'NameEmail',
|
|
'string',
|
|
'{"format": "name-email"}',
|
|
'Pydantic standard "format" extension',
|
|
''
|
|
],
|
|
[
|
|
'UrlStr',
|
|
'string',
|
|
'{"format": "uri"}',
|
|
'JSON Schema Validation',
|
|
''
|
|
],
|
|
[
|
|
'DSN',
|
|
'string',
|
|
'{"format": "dsn"}',
|
|
'Pydantic standard "format" extension',
|
|
''
|
|
],
|
|
[
|
|
'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',
|
|
'string',
|
|
'{"format": "time-delta"}',
|
|
'Pydantic standard "format" extension',
|
|
'Suggested in JSON Schema repository\'s issues by maintainer.'
|
|
],
|
|
[
|
|
'Json',
|
|
'string',
|
|
'{"format": "json-string"}',
|
|
'Pydantic standard "format" extension',
|
|
''
|
|
],
|
|
[
|
|
'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)',
|
|
'integer',
|
|
'{"maximum": 5, "exclusiveMaximum": 6, "minimum": 2, "exclusiveMinimum": 1}',
|
|
'',
|
|
'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)',
|
|
'number',
|
|
'{"maximum": 5, "exclusiveMaximum": 6, "minimum": 2, "exclusiveMinimum": 1}',
|
|
'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)',
|
|
'number',
|
|
'{"maximum": 5, "exclusiveMaximum": 6, "minimum": 2, "exclusiveMinimum": 1}',
|
|
'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.'
|
|
]
|
|
]
|
|
|
|
headings = [
|
|
'Python type',
|
|
'JSON Schema Type',
|
|
'Additional JSON Schema',
|
|
'Defined in',
|
|
'Notes',
|
|
]
|
|
|
|
v = ''
|
|
col_width = 300
|
|
for _ in range(5):
|
|
v += '+' + '-' * col_width
|
|
v += '+\n|'
|
|
for heading in headings:
|
|
v += f' {heading:{col_width - 2}} |'
|
|
v += '\n'
|
|
for _ in range(5):
|
|
v += '+' + '=' * col_width
|
|
v += '+'
|
|
for row in table:
|
|
v += '\n|'
|
|
for i, text in enumerate(row):
|
|
text = f'``{text}``' if i < 3 and text else text
|
|
v += f' {text:{col_width - 2}} |'
|
|
v += '\n'
|
|
for _ in range(5):
|
|
v += '+' + '-' * col_width
|
|
v += '+'
|
|
|
|
with open('.tmp_schema_mappings.rst', 'w') as f:
|
|
f.write(v)
|