JSON Schema
Usage Documentation
The json_schema
module contains classes and functions to allow the way JSON Schema
is generated to be customized.
In general you shouldn't need to use this module directly; instead, you can use
BaseModel.model_json_schema
and
TypeAdapter.json_schema
.
CoreSchemaOrFieldType
module-attribute
¶
CoreSchemaOrFieldType = Literal[
CoreSchemaType, CoreSchemaFieldType
]
A type alias for defined schema types that represents a union of
core_schema.CoreSchemaType
and
core_schema.CoreSchemaFieldType
.
JsonSchemaValue
module-attribute
¶
A type alias for a JSON schema value. This is a dictionary of string keys to arbitrary JSON values.
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.
JsonSchemaWarningKind
module-attribute
¶
JsonSchemaWarningKind = Literal[
"skipped-choice",
"non-serializable-default",
"skipped-discriminator",
]
A type alias representing the kinds of warnings that can be emitted during JSON schema generation.
See GenerateJsonSchema.render_warning_message
for more details.
DEFAULT_REF_TEMPLATE
module-attribute
¶
DEFAULT_REF_TEMPLATE = '#/$defs/{model}'
The default format string used to generate reference names.
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.
GenerateJsonSchema ¶
Usage Documentation
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 to use field aliases 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. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias |
bool
|
Whether to use field aliases in the generated schemas. |
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 reused after generating a schema. |
Source code in pydantic/json_schema.py
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 |
|
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.
build_schema_type_to_method ¶
build_schema_type_to_method() -> dict[
CoreSchemaOrFieldType,
Callable[[CoreSchemaOrField], JsonSchemaValue],
]
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
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 |
|
generate_definitions ¶
generate_definitions(
inputs: Sequence[
tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]
]
) -> tuple[
dict[
tuple[JsonSchemaKeyT, JsonSchemaMode],
JsonSchemaValue,
],
dict[DefsRef, JsonSchemaValue],
]
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, 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
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 |
|
generate ¶
generate(
schema: CoreSchema, mode: JsonSchemaMode = "validation"
) -> JsonSchemaValue
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
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 |
|
generate_inner ¶
generate_inner(
schema: CoreSchemaOrField,
) -> JsonSchemaValue
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. |
TODO: the nested function definitions here seem like bad practice, I'd like to unpack these in a future PR. It'd be great if we could shorten the call stack a bit for JSON schema generation, and I think there's potential for that here.
Source code in pydantic/json_schema.py
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 |
|
sort ¶
sort(
value: JsonSchemaValue, parent_key: str | None = None
) -> JsonSchemaValue
Override this method to customize the sorting of the JSON schema (e.g., don't sort at all, sort all keys unconditionally, etc.)
By default, alphabetically sort the keys in the JSON schema, skipping the 'properties' and 'default' keys to preserve field definition order. This sort is recursive, so it will sort all nested dictionaries as well.
Source code in pydantic/json_schema.py
570 571 572 573 574 575 576 577 578 579 580 581 582 |
|
invalid_schema ¶
invalid_schema(schema: InvalidSchema) -> JsonSchemaValue
Placeholder - should never be called.
Source code in pydantic/json_schema.py
604 605 606 607 |
|
any_schema ¶
any_schema(schema: AnySchema) -> JsonSchemaValue
Generates a JSON schema that matches any value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
AnySchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
609 610 611 612 613 614 615 616 617 618 |
|
none_schema ¶
none_schema(schema: NoneSchema) -> JsonSchemaValue
Generates a JSON schema that matches None
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
NoneSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
620 621 622 623 624 625 626 627 628 629 |
|
bool_schema ¶
bool_schema(schema: BoolSchema) -> JsonSchemaValue
Generates a JSON schema that matches a bool value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BoolSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
631 632 633 634 635 636 637 638 639 640 |
|
int_schema ¶
int_schema(schema: IntSchema) -> JsonSchemaValue
Generates a JSON schema that matches an int value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IntSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
642 643 644 645 646 647 648 649 650 651 652 653 654 |
|
float_schema ¶
float_schema(schema: FloatSchema) -> JsonSchemaValue
Generates a JSON schema that matches a float value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
FloatSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
decimal_schema ¶
decimal_schema(schema: DecimalSchema) -> JsonSchemaValue
Generates a JSON schema that matches a decimal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DecimalSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
|
str_schema ¶
str_schema(schema: StringSchema) -> JsonSchemaValue
Generates a JSON schema that matches a string value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
StringSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
|
bytes_schema ¶
bytes_schema(schema: BytesSchema) -> JsonSchemaValue
Generates a JSON schema that matches a bytes value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BytesSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
719 720 721 722 723 724 725 726 727 728 729 730 |
|
date_schema ¶
date_schema(schema: DateSchema) -> JsonSchemaValue
Generates a JSON schema that matches a date value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DateSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
732 733 734 735 736 737 738 739 740 741 |
|
time_schema ¶
time_schema(schema: TimeSchema) -> JsonSchemaValue
Generates a JSON schema that matches a time value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
743 744 745 746 747 748 749 750 751 752 |
|
datetime_schema ¶
datetime_schema(schema: DatetimeSchema) -> JsonSchemaValue
Generates a JSON schema that matches a datetime value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DatetimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
754 755 756 757 758 759 760 761 762 763 |
|
timedelta_schema ¶
timedelta_schema(
schema: TimedeltaSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a timedelta value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TimedeltaSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
765 766 767 768 769 770 771 772 773 774 775 776 |
|
literal_schema ¶
literal_schema(schema: LiteralSchema) -> JsonSchemaValue
Generates a JSON schema that matches a literal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
LiteralSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
|
enum_schema ¶
enum_schema(schema: EnumSchema) -> JsonSchemaValue
Generates a JSON schema that matches an Enum value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
EnumSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
|
is_instance_schema ¶
is_instance_schema(
schema: IsInstanceSchema,
) -> JsonSchemaValue
Handles JSON schema generation for a core schema that checks if a value is an instance of a class.
Unless overridden in a subclass, this raises an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IsInstanceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
848 849 850 851 852 853 854 855 856 857 858 859 |
|
is_subclass_schema ¶
is_subclass_schema(
schema: IsSubclassSchema,
) -> JsonSchemaValue
Handles JSON schema generation for a core schema that checks if a value is a subclass of a class.
For backwards compatibility with v1, this does not raise an error, but can be overridden to change this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IsSubclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
861 862 863 864 865 866 867 868 869 870 871 872 873 |
|
callable_schema ¶
callable_schema(schema: CallableSchema) -> JsonSchemaValue
Generates a JSON schema that matches a callable value.
Unless overridden in a subclass, this raises an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CallableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
875 876 877 878 879 880 881 882 883 884 885 886 |
|
list_schema ¶
list_schema(schema: ListSchema) -> JsonSchemaValue
Returns a schema that matches a list schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ListSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
888 889 890 891 892 893 894 895 896 897 898 899 900 |
|
tuple_positional_schema ¶
tuple_positional_schema(
schema: TupleSchema,
) -> JsonSchemaValue
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
902 903 904 905 906 907 908 909 910 911 |
|
tuple_variable_schema ¶
tuple_variable_schema(
schema: TupleSchema,
) -> JsonSchemaValue
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
913 914 915 916 917 918 919 920 921 922 |
|
tuple_schema ¶
tuple_schema(schema: TupleSchema) -> JsonSchemaValue
Generates a JSON schema that matches a tuple schema e.g. Tuple[int,
str, bool]
or Tuple[int, ...]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TupleSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 |
|
set_schema ¶
set_schema(schema: SetSchema) -> JsonSchemaValue
Generates a JSON schema that matches a set schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SetSchema
|
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 |
|
frozenset_schema ¶
frozenset_schema(
schema: FrozenSetSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a frozenset schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
FrozenSetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
970 971 972 973 974 975 976 977 978 979 |
|
generator_schema ¶
generator_schema(
schema: GeneratorSchema,
) -> JsonSchemaValue
Returns a JSON schema that represents the provided GeneratorSchema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
GeneratorSchema
|
The schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
987 988 989 990 991 992 993 994 995 996 997 998 999 |
|
dict_schema ¶
dict_schema(schema: DictSchema) -> JsonSchemaValue
Generates a JSON schema that matches a dict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
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 1026 1027 1028 1029 1030 1031 1032 1033 |
|
function_before_schema ¶
function_before_schema(
schema: BeforeValidatorFunctionSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a function-before schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BeforeValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
|
function_after_schema ¶
function_after_schema(
schema: AfterValidatorFunctionSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a function-after schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
AfterValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
|
function_plain_schema ¶
function_plain_schema(
schema: PlainValidatorFunctionSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a function-plain schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
PlainValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 |
|
function_wrap_schema ¶
function_wrap_schema(
schema: WrapValidatorFunctionSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a function-wrap schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
WrapValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 |
|
default_schema ¶
default_schema(
schema: WithDefaultSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema with a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
WithDefaultSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 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 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 |
|
nullable_schema ¶
nullable_schema(schema: NullableSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that allows null values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
NullableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
|
union_schema ¶
union_schema(schema: UnionSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that allows values matching any of the given schemas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 |
|
tagged_union_schema ¶
tagged_union_schema(
schema: TaggedUnionSchema,
) -> JsonSchemaValue
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 |
TaggedUnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 |
|
chain_schema ¶
chain_schema(schema: ChainSchema) -> JsonSchemaValue
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 |
ChainSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(
schema: LaxOrStrictSchema,
) -> JsonSchemaValue
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 |
LaxOrStrictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 |
|
json_or_python_schema ¶
json_or_python_schema(
schema: JsonOrPythonSchema,
) -> JsonSchemaValue
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 |
JsonOrPythonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 |
|
typed_dict_schema ¶
typed_dict_schema(
schema: TypedDictSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a typed dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TypedDictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(
schema: TypedDictField,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a typed dict field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TypedDictField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 |
|
dataclass_field_schema ¶
dataclass_field_schema(
schema: DataclassField,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 |
|
model_field_schema ¶
model_field_schema(schema: ModelField) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 |
|
computed_field_schema ¶
computed_field_schema(
schema: ComputedField,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a computed field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ComputedField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 |
|
model_schema ¶
model_schema(schema: ModelSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 |
|
resolve_ref_schema ¶
resolve_ref_schema(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
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
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 |
|
model_fields_schema ¶
model_fields_schema(
schema: ModelFieldsSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model's fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelFieldsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 |
|
field_is_present ¶
field_is_present(field: CoreSchemaField) -> bool
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
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 |
|
field_is_required ¶
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 |
ModelField | DataclassField | 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
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 |
|
dataclass_args_schema ¶
dataclass_args_schema(
schema: DataclassArgsSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass's constructor arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassArgsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 |
|
dataclass_schema ¶
dataclass_schema(
schema: DataclassSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 |
|
arguments_schema ¶
arguments_schema(
schema: ArgumentsSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ArgumentsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 |
|
kw_arguments_schema ¶
kw_arguments_schema(
arguments: list[ArgumentsParameter],
var_kwargs_schema: CoreSchema | None,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
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 1750 1751 1752 1753 1754 1755 1756 1757 |
|
p_arguments_schema ¶
p_arguments_schema(
arguments: list[ArgumentsParameter],
var_args_schema: CoreSchema | None,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's positional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1759 1760 1761 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 |
|
get_argument_name ¶
get_argument_name(argument: ArgumentsParameter) -> str
Retrieves the name of an argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
argument |
ArgumentsParameter
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the argument. |
Source code in pydantic/json_schema.py
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 |
|
call_schema ¶
call_schema(schema: CallSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CallSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 |
|
custom_error_schema ¶
custom_error_schema(
schema: CustomErrorSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a custom error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CustomErrorSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 |
|
json_schema ¶
json_schema(schema: JsonSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a JSON object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 |
|
url_schema ¶
url_schema(schema: UrlSchema) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 |
|
multi_host_url_schema ¶
multi_host_url_schema(
schema: MultiHostUrlSchema,
) -> JsonSchemaValue
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 |
MultiHostUrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 |
|
uuid_schema ¶
uuid_schema(schema: UuidSchema) -> JsonSchemaValue
Generates a JSON schema that matches a UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UuidSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 |
|
definitions_schema ¶
definitions_schema(
schema: DefinitionsSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that defines a JSON object with definitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DefinitionsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
|
definition_ref_schema ¶
definition_ref_schema(
schema: DefinitionReferenceSchema,
) -> JsonSchemaValue
Generates a JSON schema that matches a schema that references a definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DefinitionReferenceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 |
|
ser_schema ¶
ser_schema(
schema: (
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
),
) -> JsonSchemaValue | None
Generates a JSON schema that matches a schema that defines a serialized object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue | None
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 |
|
complex_schema ¶
complex_schema(schema: ComplexSchema) -> JsonSchemaValue
Generates a JSON schema that matches a complex number.
JSON has no standard way to represent complex numbers. Complex number is not a numeric
type. Here we represent complex number as strings following the rule defined by Python.
For instance, '1+2j' is an accepted complex string. Details can be found in
Python's complex
documentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ComplexSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 |
|
get_title_from_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
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 |
|
field_title_should_be_set ¶
field_title_should_be_set(
schema: CoreSchemaOrField,
) -> bool
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
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 |
|
normalize_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
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref: CoreModeRef) -> DefsRef
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
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 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 |
|
get_cache_defs_ref_schema ¶
get_cache_defs_ref_schema(
core_ref: CoreRef,
) -> tuple[DefsRef, JsonSchemaValue]
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
2075 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 |
|
handle_ref_overrides ¶
handle_ref_overrides(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
Remove any sibling keys that are redundant with the referenced schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
JsonSchemaValue
|
The schema to remove redundant sibling keys from. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The schema with redundant sibling keys removed. |
Source code in pydantic/json_schema.py
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 |
|
encode_default ¶
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
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 |
|
update_with_validations ¶
update_with_validations(
json_schema: JsonSchemaValue,
core_schema: CoreSchema,
mapping: dict[str, str],
) -> None
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
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 |
|
get_json_ref_counts ¶
get_json_ref_counts(
json_schema: JsonSchemaValue,
) -> dict[JsonRef, int]
Get all values corresponding to the key '$ref' anywhere in the json_schema.
Source code in pydantic/json_schema.py
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 |
|
emit_warning ¶
emit_warning(
kind: JsonSchemaWarningKind, detail: str
) -> None
This method simply emits PydanticJsonSchemaWarnings based on handling in the warning_message
method.
Source code in pydantic/json_schema.py
2266 2267 2268 2269 2270 |
|
render_warning_message ¶
render_warning_message(
kind: JsonSchemaWarningKind, detail: str
) -> str | None
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
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 |
|
WithJsonSchema
dataclass
¶
WithJsonSchema(
json_schema: JsonSchemaValue | None,
mode: (
Literal["validation", "serialization"] | None
) = None,
)
Usage Documentation
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.
Examples ¶
Examples(
examples: dict[str, Any] | list[Any],
mode: (
Literal["validation", "serialization"] | None
) = None,
)
Add examples to a JSON schema.
If the JSON Schema already contains examples, the provided examples will be appended.
If mode
is set this will only apply to that schema generation mode,
allowing you to add different examples for validation and serialization.
Source code in pydantic/json_schema.py
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 |
|
SkipJsonSchema
dataclass
¶
SkipJsonSchema()
Usage Documentation
Add this as an annotation on a field to skip generating a JSON schema for that field.
Example
from typing import Union
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema
from pprint import pprint
class Model(BaseModel):
a: Union[int, None] = None # (1)!
b: Union[int, SkipJsonSchema[None]] = None # (2)!
c: SkipJsonSchema[Union[int, None]] = None # (3)!
pprint(Model.model_json_schema())
'''
{
'properties': {
'a': {
'anyOf': [
{'type': 'integer'},
{'type': 'null'}
],
'default': None,
'title': 'A'
},
'b': {
'default': None,
'title': 'B',
'type': 'integer'
}
},
'title': 'Model',
'type': 'object'
}
'''
- The integer and null types are both included in the schema for
a
. - The integer type is the only type included in the schema for
b
. - The entirety of the
c
field is omitted from the schema.
model_json_schema ¶
model_json_schema(
cls: type[BaseModel] | type[PydanticDataclass],
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema,
mode: JsonSchemaMode = "validation",
) -> dict[str, Any]
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
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 |
|
models_json_schema ¶
models_json_schema(
models: Sequence[
tuple[
type[BaseModel] | type[PydanticDataclass],
JsonSchemaMode,
]
],
*,
by_alias: bool = True,
title: str | None = None,
description: str | None = None,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema
) -> tuple[
dict[
tuple[
type[BaseModel] | type[PydanticDataclass],
JsonSchemaMode,
],
JsonSchemaValue,
],
JsonSchemaValue,
]
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
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 |
|