pydantic.json_schema
The json_schema
module contains classes and functions for generating JSON schemas.
CoreSchemaOrFieldType
module-attribute
¶
CoreSchemaOrFieldType = Literal[
core_schema.CoreSchemaType,
core_schema.CoreSchemaFieldType,
]
A type alias for defined schema types that represents a union of
core_schema.CoreSchemaType
and
core_schema.CoreSchemaFieldType
.
DEFAULT_REF_TEMPLATE
module-attribute
¶
DEFAULT_REF_TEMPLATE = '#/$defs/{model}'
The default format string used to generate reference names.
JsonSchemaMode
module-attribute
¶
JsonSchemaMode = Literal['validation', 'serialization']
A type alias that represents the mode of a JSON schema; either 'validation' or 'serialization'.
For some types, the inputs to validation differ from the outputs of serialization. For example, computed fields will only be present when serializing, and should not be provided when validating. This flag provides a way to indicate whether you want the JSON schema required for validation inputs, or that will be matched by serialization outputs.
JsonSchemaValue
module-attribute
¶
JsonSchemaValue = Dict[str, Any]
A type alias for a JSON schema value. This is a dictionary of string keys to arbitrary values.
JsonSchemaWarningKind
module-attribute
¶
JsonSchemaWarningKind = Literal[
"skipped-choice", "non-serializable-default"
]
A type alias representing the kinds of warnings that can be emitted during JSON schema generation.
See GenerateJsonSchema.render_warning_message
for more details.
Examples
dataclass
¶
Add examples to a JSON schema.
Examples should be a map of example names (strings) to example values (any valid JSON).
If mode
is set this will only apply to that schema generation mode,
allowing you to add different examples for validation and serialization.
GenerateJsonSchema ¶
GenerateJsonSchema(
by_alias=True, ref_template=DEFAULT_REF_TEMPLATE
)
A class for generating JSON schemas.
This class generates JSON schemas based on configured parameters. The default schema dialect
is 'https://json-schema.org/draft/2020-12/schema'. The class uses by_alias
to configure how fields with
multiple names are handled and ref_template
to format reference names.
Attributes:
Name | Type | Description |
---|---|---|
schema_dialect |
The JSON schema dialect used to generate the schema. See Declaring a Dialect in the JSON Schema documentation for more information about dialects. |
|
ignored_warning_kinds |
set[JsonSchemaWarningKind]
|
Warnings to ignore when generating the schema. |
by_alias |
Whether or not to use field names when generating the schema. |
|
ref_template |
The format string used when generating reference names. |
|
core_to_json_refs |
dict[CoreModeRef, JsonRef]
|
A mapping of core refs to JSON refs. |
core_to_defs_refs |
dict[CoreModeRef, DefsRef]
|
A mapping of core refs to definition refs. |
defs_to_core_refs |
dict[DefsRef, CoreModeRef]
|
A mapping of definition refs to core refs. |
json_to_defs_refs |
dict[JsonRef, DefsRef]
|
A mapping of JSON refs to definition refs. |
definitions |
dict[DefsRef, JsonSchemaValue]
|
Definitions in the schema. |
collisions |
dict[DefsRef, JsonSchemaValue]
|
Definitions with colliding names. When collisions are detected, we choose a non-colliding name during generation, but we also track the colliding tag so that it can be remapped for the first occurrence at the end of the process. |
defs_ref_fallbacks |
dict[DefsRef, JsonSchemaValue]
|
Core refs to fallback definitions refs. |
_schema_type_to_method |
A mapping of schema types to generator methods. |
|
_used |
Set to |
|
mode |
JsonSchemaMode
|
The schema mode. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias |
bool
|
Whether or not to include field names. |
True
|
ref_template |
str
|
The format string to use when generating reference names. |
DEFAULT_REF_TEMPLATE
|
Raises:
Type | Description |
---|---|
JsonSchemaError
|
If the instance of the class is inadvertently re-used after generating a schema. |
Source code in pydantic/json_schema.py
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 |
|
ValidationsMapping ¶
This class just contains mappings from core_schema attribute names to the corresponding JSON schema attribute names. While I suspect it is unlikely to be necessary, you can in principle override this class in a subclass of GenerateJsonSchema (by inheriting from GenerateJsonSchema.ValidationsMapping) to change these mappings.
any_schema ¶
any_schema(schema)
Generates a JSON schema that matches any value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.AnySchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
527 528 529 530 531 532 533 534 535 536 |
|
arguments_schema ¶
arguments_schema(schema)
Generates a JSON schema that matches a schema that defines a function's arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ArgumentsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 |
|
bool_schema ¶
bool_schema(schema)
Generates a JSON schema that matches a bool value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BoolSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
549 550 551 552 553 554 555 556 557 558 |
|
build_schema_type_to_method ¶
build_schema_type_to_method()
Builds a dictionary mapping fields to methods for generating JSON schemas.
Returns:
Type | Description |
---|---|
dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]]
|
A dictionary containing the mapping of |
Raises:
Type | Description |
---|---|
TypeError
|
If no method has been defined for generating a JSON schema for a given pydantic core schema type. |
Source code in pydantic/json_schema.py
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 |
|
bytes_schema ¶
bytes_schema(schema)
Generates a JSON schema that matches a bytes value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BytesSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
601 602 603 604 605 606 607 608 609 610 611 612 |
|
call_schema ¶
call_schema(schema)
Generates a JSON schema that matches a schema that defines a function call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.CallSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 |
|
callable_schema ¶
callable_schema(schema)
Generates a JSON schema that matches a callable value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.CallableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
715 716 717 718 719 720 721 722 723 724 |
|
chain_schema ¶
chain_schema(schema)
Generates a JSON schema that matches a core_schema.ChainSchema.
When generating a schema for validation, we return the validation JSON schema for the first step in the chain. For serialization, we return the serialization JSON schema for the last step in the chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ChainSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
|
computed_field_schema ¶
computed_field_schema(schema)
Generates a JSON schema that matches a schema that defines a computed field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ComputedField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 |
|
custom_error_schema ¶
custom_error_schema(schema)
Generates a JSON schema that matches a schema that defines a custom error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.CustomErrorSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 |
|
dataclass_args_schema ¶
dataclass_args_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass's constructor arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DataclassArgsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 |
|
dataclass_field_schema ¶
dataclass_field_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DataclassField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
|
dataclass_schema ¶
dataclass_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DataclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 |
|
date_schema ¶
date_schema(schema)
Generates a JSON schema that matches a date value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DateSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
614 615 616 617 618 619 620 621 622 623 624 625 |
|
datetime_schema ¶
datetime_schema(schema)
Generates a JSON schema that matches a datetime value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DatetimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
638 639 640 641 642 643 644 645 646 647 |
|
default_schema ¶
default_schema(schema)
Generates a JSON schema that matches a schema with a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.WithDefaultSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
|
definition_ref_schema ¶
definition_ref_schema(schema)
Generates a JSON schema that matches a schema that references a definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DefinitionReferenceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 |
|
definitions_schema ¶
definitions_schema(schema)
Generates a JSON schema that matches a schema that defines a JSON object with definitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DefinitionsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 |
|
dict_schema ¶
dict_schema(schema)
Generates a JSON schema that matches a dict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
|
emit_warning ¶
emit_warning(kind, detail)
This method simply emits PydanticJsonSchemaWarnings based on handling in the warning_message
method.
Source code in pydantic/json_schema.py
1994 1995 1996 1997 1998 |
|
encode_default ¶
encode_default(dft)
Encode a default value to a JSON-serializable value.
This is used to encode default values for fields in the generated JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dft |
Any
|
The default value to encode. |
required |
Returns:
Type | Description |
---|---|
Any
|
The encoded default value. |
Source code in pydantic/json_schema.py
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 |
|
field_is_present ¶
field_is_present(field)
Whether the field should be included in the generated JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
CoreSchemaField
|
The schema for the field itself. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
|
field_is_required ¶
field_is_required(field, total)
Whether the field should be marked as required in the generated JSON schema. (Note that this is irrelevant if the field is not present in the JSON schema.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
core_schema.ModelField | core_schema.DataclassField | core_schema.TypedDictField
|
The schema for the field itself. |
required |
total |
bool
|
Only applies to |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 |
|
field_title_should_be_set ¶
field_title_should_be_set(schema)
Returns true if a field with the given schema should have a title set based on the field name.
Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title (e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchemaOrField
|
The schema to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 |
|
float_schema ¶
float_schema(schema)
Generates a JSON schema that matches a float value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.FloatSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
574 575 576 577 578 579 580 581 582 583 584 585 586 |
|
frozenset_schema ¶
frozenset_schema(schema)
Generates a JSON schema that matches a frozenset schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.FrozenSetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
786 787 788 789 790 791 792 793 794 795 |
|
function_after_schema ¶
function_after_schema(schema)
Generates a JSON schema that matches a function-after schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.AfterValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
867 868 869 870 871 872 873 874 875 876 |
|
function_before_schema ¶
function_before_schema(schema)
Generates a JSON schema that matches a function-before schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BeforeValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
856 857 858 859 860 861 862 863 864 865 |
|
function_plain_schema ¶
function_plain_schema(schema)
Generates a JSON schema that matches a function-plain schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.PlainValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
878 879 880 881 882 883 884 885 886 887 |
|
function_wrap_schema ¶
function_wrap_schema(schema)
Generates a JSON schema that matches a function-wrap schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.WrapValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
889 890 891 892 893 894 895 896 897 898 |
|
generate ¶
generate(schema, mode='validation')
Generates a JSON schema for a specified schema in a specified mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
A Pydantic model. |
required |
mode |
JsonSchemaMode
|
The mode in which to generate the schema. Defaults to 'validation'. |
'validation'
|
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
A JSON schema representing the specified schema. |
Raises:
Type | Description |
---|---|
PydanticUserError
|
If the JSON schema generator has already been used to generate a JSON schema. |
Source code in pydantic/json_schema.py
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 |
|
generate_definitions ¶
generate_definitions(inputs)
Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a mapping that links the input keys to the definition references.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]]
|
A sequence of tuples, where:
|
required |
Returns:
Type | Description |
---|---|
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]
|
A tuple where:
|
Raises:
Type | Description |
---|---|
PydanticUserError
|
Raised if the JSON schema generator has already been used to generate a JSON schema. |
Source code in pydantic/json_schema.py
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 |
|
generate_inner ¶
generate_inner(schema)
Generates a JSON schema for a given core schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchemaOrField
|
The given core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
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 |
|
generator_schema ¶
generator_schema(schema)
Returns a JSON schema that represents the provided GeneratorSchema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.GeneratorSchema
|
The schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
803 804 805 806 807 808 809 810 811 812 813 814 815 |
|
get_argument_name ¶
get_argument_name(argument)
Retrieves the name of an argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
argument |
core_schema.ArgumentsParameter
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the argument. |
Source code in pydantic/json_schema.py
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 |
|
get_cache_defs_ref_schema ¶
get_cache_defs_ref_schema(core_ref)
This method wraps the get_defs_ref method with some cache-lookup/population logic, and returns both the produced defs_ref and the JSON schema that will refer to the right definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
core_ref |
CoreRef
|
The core reference to get the definitions reference for. |
required |
Returns:
Type | Description |
---|---|
tuple[DefsRef, JsonSchemaValue]
|
A tuple of the definitions reference and the JSON schema that will refer to it. |
Source code in pydantic/json_schema.py
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref)
Override this method to change the way that definitions keys are generated from a core reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
core_mode_ref |
CoreModeRef
|
The core reference. |
required |
Returns:
Type | Description |
---|---|
DefsRef
|
The definitions key. |
Source code in pydantic/json_schema.py
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 |
|
get_json_ref_counts ¶
get_json_ref_counts(json_schema)
Get all values corresponding to the key '$ref' anywhere in the json_schema.
Source code in pydantic/json_schema.py
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 |
|
get_title_from_name ¶
get_title_from_name(name)
Retrieves a title from a name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to retrieve a title from. |
required |
Returns:
Type | Description |
---|---|
str
|
The title. |
Source code in pydantic/json_schema.py
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 |
|
handle_ref_overrides ¶
handle_ref_overrides(json_schema)
It is not valid for a schema with a top-level $ref to have sibling keys.
During our own schema generation, we treat sibling keys as overrides to the referenced schema, but this is not how the official JSON schema spec works.
Because of this, we first remove any sibling keys that are redundant with the referenced schema, then if any remain, we transform the schema from a top-level '$ref' to use allOf to move the $ref out of the top level. (See bottom of https://swagger.io/docs/specification/using-ref/ for a reference about this behavior)
Source code in pydantic/json_schema.py
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 |
|
int_schema ¶
int_schema(schema)
Generates a JSON schema that matches an Int value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IntSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
560 561 562 563 564 565 566 567 568 569 570 571 572 |
|
is_instance_schema ¶
is_instance_schema(schema)
Generates a JSON schema that checks if a value is an instance of a class, equivalent to Python's
isinstance
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IsInstanceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
690 691 692 693 694 695 696 697 698 699 700 |
|
is_subclass_schema ¶
is_subclass_schema(schema)
Generates a JSON schema that checks if a value is a subclass of a class, equivalent to Python's issubclass
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IsSubclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
702 703 704 705 706 707 708 709 710 711 712 713 |
|
json_or_python_schema ¶
json_or_python_schema(schema)
Generates a JSON schema that matches a schema that allows values matching either the JSON schema or the Python schema.
The JSON schema is used instead of the Python schema. If you want to use the Python schema, you should override this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.JsonOrPythonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 |
|
json_schema ¶
json_schema(schema)
Generates a JSON schema that matches a schema that defines a JSON object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.JsonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 |
|
kw_arguments_schema ¶
kw_arguments_schema(arguments, var_kwargs_schema)
Generates a JSON schema that matches a schema that defines a function's keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[core_schema.ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(schema)
Generates a JSON schema that matches a schema that allows values matching either the lax schema or the strict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.LaxOrStrictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
|
list_schema ¶
list_schema(schema)
Returns a schema that matches a list schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ListSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
726 727 728 729 730 731 732 733 734 735 736 737 738 |
|
literal_schema ¶
literal_schema(schema)
Generates a JSON schema that matches a literal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.LiteralSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
model_field_schema ¶
model_field_schema(schema)
Generates a JSON schema that matches a schema that defines a model field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ModelField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
|
model_fields_schema ¶
model_fields_schema(schema)
Generates a JSON schema that matches a schema that defines a model's fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ModelFieldsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
|
model_schema ¶
model_schema(schema)
Generates a JSON schema that matches a schema that defines a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ModelSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 |
|
multi_host_url_schema ¶
multi_host_url_schema(schema)
Generates a JSON schema that matches a schema that defines a URL that can be used with multiple hosts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.MultiHostUrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 |
|
none_schema ¶
none_schema(schema)
Generates a JSON schema that matches a None value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.NoneSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
538 539 540 541 542 543 544 545 546 547 |
|
normalize_name ¶
normalize_name(name)
Normalizes a name to be used as a key in a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to normalize. |
required |
Returns:
Type | Description |
---|---|
str
|
The normalized name. |
Source code in pydantic/json_schema.py
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 |
|
nullable_schema ¶
nullable_schema(schema)
Generates a JSON schema that matches a schema that allows null values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.NullableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 |
|
p_arguments_schema ¶
p_arguments_schema(arguments, var_args_schema)
Generates a JSON schema that matches a schema that defines a function's positional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[core_schema.ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 |
|
render_warning_message ¶
render_warning_message(kind, detail)
This method is responsible for ignoring warnings as desired, and for formatting the warning messages.
You can override the value of ignored_warning_kinds
in a subclass of GenerateJsonSchema
to modify what warnings are generated. If you want more control, you can override this method;
just return None in situations where you don't want warnings to be emitted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kind |
JsonSchemaWarningKind
|
The kind of warning to render. It can be one of the following:
|
required |
detail |
str
|
A string with additional details about the warning. |
required |
Returns:
Type | Description |
---|---|
str | None
|
The formatted warning message, or |
Source code in pydantic/json_schema.py
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 |
|
resolve_schema_to_update ¶
resolve_schema_to_update(json_schema)
Resolve a JsonSchemaValue to the non-ref schema if it is a $ref schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
JsonSchemaValue
|
The schema to resolve. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The resolved schema. |
Source code in pydantic/json_schema.py
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 |
|
ser_schema ¶
ser_schema(schema)
Generates a JSON schema that matches a schema that defines a serialized object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.SerSchema | core_schema.IncExSeqSerSchema | core_schema.IncExDictSerSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue | None
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 |
|
set_schema ¶
set_schema(schema)
Generates a JSON schema that matches a set schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.SetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
775 776 777 778 779 780 781 782 783 784 |
|
str_schema ¶
str_schema(schema)
Generates a JSON schema that matches a string value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.StringSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
588 589 590 591 592 593 594 595 596 597 598 599 |
|
tagged_union_schema ¶
tagged_union_schema(schema)
Generates a JSON schema that matches a schema that allows values matching any of the given schemas, where the schemas are tagged with a discriminator field that indicates which schema should be used to validate the value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TaggedUnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 |
|
time_schema ¶
time_schema(schema)
Generates a JSON schema that matches a time value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
627 628 629 630 631 632 633 634 635 636 |
|
timedelta_schema ¶
timedelta_schema(schema)
Generates a JSON schema that matches a timedelta value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TimedeltaSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
649 650 651 652 653 654 655 656 657 658 |
|
tuple_positional_schema ¶
tuple_positional_schema(schema)
Generates a JSON schema that matches a positional tuple schema e.g. Tuple[int, str, bool]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TuplePositionalSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
|
tuple_variable_schema ¶
tuple_variable_schema(schema)
Generates a JSON schema that matches a variable tuple schema e.g. Tuple[int, ...]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TupleVariableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
761 762 763 764 765 766 767 768 769 770 771 772 773 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(schema)
Generates a JSON schema that matches a schema that defines a typed dict field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TypedDictField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 |
|
typed_dict_schema ¶
typed_dict_schema(schema)
Generates a JSON schema that matches a schema that defines a typed dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TypedDictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
|
union_schema ¶
union_schema(schema)
Generates a JSON schema that matches a schema that allows values matching any of the given schemas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.UnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
|
update_with_validations ¶
update_with_validations(json_schema, core_schema, mapping)
Update the json_schema with the corresponding validations specified in the core_schema, using the provided mapping to translate keys in core_schema to the appropriate keys for a JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
JsonSchemaValue
|
The JSON schema to update. |
required |
core_schema |
CoreSchema
|
The core schema to get the validations from. |
required |
mapping |
dict[str, str]
|
A mapping from core_schema attribute names to the corresponding JSON schema attribute names. |
required |
Source code in pydantic/json_schema.py
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 |
|
url_schema ¶
url_schema(schema)
Generates a JSON schema that matches a schema that defines a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.UrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 |
|
PydanticJsonSchemaWarning ¶
Bases: UserWarning
This class is used to emit warnings produced during JSON schema generation.
See the GenerateJsonSchema.emit_warning
and GenerateJsonSchema.render_warning_message
methods for more details; these can be overridden to control warning behavior.
WithJsonSchema
dataclass
¶
Add this as an annotation on a field to override the (base) JSON schema that would be generated for that field. This provides a way to set a JSON schema for types that would otherwise raise errors when producing a JSON schema, such as Callable, or types that have an is-instance core schema, without needing to go so far as creating a custom subclass of pydantic.json_schema.GenerateJsonSchema. Note that any modifications to the schema that would normally be made (such as setting the title for model fields) will still be performed.
If mode
is set this will only apply to that schema generation mode, allowing you
to set different json schemas for validation and serialization.
model_json_schema ¶
model_json_schema(
cls,
by_alias=True,
ref_template=DEFAULT_REF_TEMPLATE,
schema_generator=GenerateJsonSchema,
mode="validation",
)
Utility function to generate a JSON Schema for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type[BaseModel] | type[PydanticDataclass]
|
The model class to generate a JSON Schema for. |
required |
by_alias |
bool
|
If |
True
|
ref_template |
str
|
The template to use for generating JSON Schema references. |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
The class to use for generating the JSON Schema. |
GenerateJsonSchema
|
mode |
JsonSchemaMode
|
The mode to use for generating the JSON Schema. It can be one of the following:
|
'validation'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The generated JSON Schema. |
Source code in pydantic/json_schema.py
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 |
|
models_json_schema ¶
models_json_schema(
models,
*,
by_alias=True,
title=None,
description=None,
ref_template=DEFAULT_REF_TEMPLATE,
schema_generator=GenerateJsonSchema
)
Utility function to generate a JSON Schema for multiple models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models |
Sequence[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode]]
|
A sequence of tuples of the form (model, mode). |
required |
by_alias |
bool
|
Whether field aliases should be used as keys in the generated JSON Schema. |
True
|
title |
str | None
|
The title of the generated JSON Schema. |
None
|
description |
str | None
|
The description of the generated JSON Schema. |
None
|
ref_template |
str
|
The reference template to use for generating JSON Schema references. |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
The schema generator to use for generating the JSON Schema. |
GenerateJsonSchema
|
Returns:
Type | Description |
---|---|
tuple[dict[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]
|
A tuple where: - The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and whose values are the JSON schema corresponding to that pair of inputs. (These schemas may have JsonRef references to definitions that are defined in the second returned element.) - The second element is a JSON schema containing all definitions referenced in the first returned element, along with the optional title and description keys. |
Source code in pydantic/json_schema.py
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 |
|
update_json_schema ¶
update_json_schema(schema, updates)
Update a JSON schema by providing a dictionary of updates.
This function sets the provided key-value pairs in the schema and returns the updated schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonSchemaValue
|
The JSON schema to update. |
required |
updates |
dict[str, Any]
|
A dictionary of key-value pairs to set in the schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The updated JSON schema. |
Source code in pydantic/json_schema.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|