* Prevent type attributes being added to schema unintentionally, fix #1064 * simpler boolean check * change to __modify_schema__ * better docs * Update docs/usage/schema.md Co-Authored-By: dmontagu <35119617+dmontagu@users.noreply.github.com>
6.5 KiB
Pydantic allows auto creation of JSON Schemas from models:
{!.tmp_examples/schema_main.py!}
(This script is complete, it should run "as is")
Outputs:
{!.tmp_examples/schema_main.json!}
The generated schemas are compliant with the specifications: JSON Schema Core, JSON Schema Validation and OpenAPI.
BaseModel.schema will return a dict of the schema, while BaseModel.schema_json will return a JSON string
representation of that dict.
Sub-models used are added to the definitions JSON attribute and referenced, as per the spec.
All sub-models' (and their sub-models') schemas are put directly in a top-level definitions JSON key for easy re-use
and reference.
"Sub-models" with modifications (via the Field class) like a custom title, description or default value,
are recursively included instead of referenced.
The description for models is taken from either the docstring of the class or the argument description to
the Field class.
The schema is generated by default using aliases as keys, but it can be generated using model
property names instead by calling MainModel.schema/schema_json(by_alias=False).
Field customisation
Optionally, the Field function can be used to provide extra information about the field and validations.
It has the following arguments:
default: (a positional argument) the default value of the field. Since theFieldreplaces the field's default, this first argument can be used to set the default. Use ellipsis (...) to indicate the field is required.alias: the public name of the fieldtitle: if omitted,field_name.title()is useddescription: if omitted and the annotation is a sub-model, the docstring of the sub-model will be usedconst: this argument must have be the same as the field's default value if presentgt: for numeric values (int,float,Decimal), adds a validation of "greater than" and an annotation ofexclusiveMinimumto the JSON Schemage: for numeric values, this adds a validation of "greater than or equal" and an annotation ofminimumto the JSON Schemalt: for numeric values, this adds a validation of "less than" and an annotation ofexclusiveMaximumto the JSON Schemale: for numeric values, this adds a validation of "less than or equal" and an annotation ofmaximumto the JSON Schemamultiple_of: for numeric values, this adds a validation of "a multiple of" and an annotation ofmultipleOfto the JSON Schemamin_items: for list values, this adds a corresponding validation and an annotation ofminItemsto the JSON Schemamax_items: for list values, this adds a corresponding validation and an annotation ofmaxItemsto the JSON Schemamin_length: for string values, this adds a corresponding validation and an annotation ofminLengthto the JSON Schemamax_length: for string values, this adds a corresponding validation and an annotation ofmaxLengthto the JSON Schemaregex: for string values, this adds a Regular Expression validation generated from the passed string and an annotation ofpatternto the JSON Schema**any other keyword arguments (e.g.examples) will be added verbatim to the field's schema
Instead of using Field, the fields property of the Config class can be used
to set all of the arguments above except default.
Unenforced Field constraints
If pydantic finds constraints which are not being enforced, an error will be raised. If you want to force the
constraint to appear in the schema, even though it's not being checked upon parsing, you can use variadic arguments
to Field() with the raw schema attribute name:
{!.tmp_examples/schema_unenforced_constraints.py!}
(This script is complete, it should run "as is")
Modifying schema in custom fields
Custom field types can customise the schema generated for them using the __modify_schema__ class method;
see Custom Data Types for more details.
JSON Schema Types
Types, custom field types, and constraints (like max_length) are mapped to the corresponding spec formats in the
following priority order (when there is an equivalent available):
- JSON Schema Core
- JSON Schema Validation
- OpenAPI Data Types
- The standard
formatJSON field is used to define pydantic extensions for more complexstringsub-types.
The field schema mapping from Python / pydantic to JSON Schema is done as follows:
{!.tmp_schema_mappings.html!}
Top-level schema generation
You can also generate a top-level JSON Schema that only includes a list of models and related
sub-models in its definitions:
{!.tmp_examples/schema_top_level.py!}
(This script is complete, it should run "as is")
Outputs:
{!.tmp_examples/schema_top_level.json!}
Schema customization
You can customize the generated $ref JSON location: the definitions are always stored under the key
definitions, but a specified prefix can be used for the references.
This is useful if you need to extend or modify the JSON Schema default definitions location. E.g. with OpenAPI:
{!.tmp_examples/schema_custom.py!}
(This script is complete, it should run "as is")
Outputs:
{!.tmp_examples/schema_custom.json!}
It's also possible to extend/override the generated JSON schema in a model.
To do it, use the Config sub-class attribute schema_extra.
For example, you could add examples to the JSON Schema:
{!.tmp_examples/schema_with_example.py!}
(This script is complete, it should run "as is")
Outputs:
{!.tmp_examples/schema_with_example.json!}
For more fine-grained control, you can alternatively set schema_extra to a callable and post-process the generated schema.
The callable is passed the schema dictionary as the first and only argument, and is expected to mutate it in-place; the return value is not used.
For example, the title key can be removed from the model's properties:
{!.tmp_examples/schema_extra_callable.py!}
(This script is complete, it should run "as is")
Outputs:
{!.tmp_examples/schema_extra_callable.json!}