JSON Schema
使用文档
json_schema
模块包含类和函数,以允许自定义 JSON Schema 的生成方式。
通常,您不应该直接使用此模块;相反,您可以使用 BaseModel.model_json_schema
和 TypeAdapter.json_schema
。
CoreSchemaOrFieldType module-attribute
¶
CoreSchemaOrFieldType = Literal[
CoreSchemaType, CoreSchemaFieldType
]
一种类型别名,用于定义模式类型,表示 core_schema.CoreSchemaType
和 core_schema.CoreSchemaFieldType
的联合。
JsonSchemaValue module-attribute
¶
JSON 模式值的类型别名。这是一个字符串键到任意 JSON 值的字典。
JsonSchemaMode module-attribute
¶
JsonSchemaMode = Literal['validation', 'serialization']
一种类型别名,表示 JSON 模式的模式;可以是“validation”或“serialization”。
对于某些类型,验证的输入与序列化的输出不同。 例如,计算字段仅在序列化时存在,并且在验证时不应提供。此标志提供了一种方法来指示您是否需要验证输入所需的 JSON 模式,或与序列化输出匹配的 JSON 模式。
JsonSchemaWarningKind module-attribute
¶
JsonSchemaWarningKind = Literal[
"skipped-choice",
"non-serializable-default",
"skipped-discriminator",
]
一种类型别名,表示在 JSON 模式生成期间可能发出的警告类型。
有关更多详细信息,请参阅 GenerateJsonSchema.render_warning_message
。
PydanticJsonSchemaWarning ¶
基类: UserWarning
此类用于发出在 JSON 模式生成期间产生的警告。 有关更多详细信息,请参阅 GenerateJsonSchema.emit_warning
和 GenerateJsonSchema.render_warning_message
方法;可以覆盖这些方法来控制警告行为。
GenerateJsonSchema ¶
GenerateJsonSchema(
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
)
使用文档
用于生成 JSON 模式的类。
此类根据配置的参数生成 JSON 模式。 默认模式方言是 https://json-schema.fullstack.org.cn/draft/2020-12/schema。此类使用 by_alias
来配置如何处理具有多个名称的字段,并使用 ref_template
来格式化引用名称。
属性
名称 | 类型 | 描述 |
---|---|---|
schema_dialect |
用于生成模式的 JSON 模式方言。 有关方言的更多信息,请参阅 JSON Schema 文档中的 声明方言。 |
|
ignored_warning_kinds |
set[JsonSchemaWarningKind]
|
生成模式时要忽略的警告。 如果 |
by_alias |
在生成模式时是否使用字段别名。 |
|
ref_template |
生成引用名称时使用的格式字符串。 |
|
core_to_json_refs |
dict[CoreModeRef, JsonRef]
|
核心引用到 JSON 引用的映射。 |
core_to_defs_refs |
dict[CoreModeRef, DefsRef]
|
核心引用到定义引用的映射。 |
defs_to_core_refs |
dict[DefsRef, CoreModeRef]
|
定义引用到核心引用的映射。 |
json_to_defs_refs |
dict[JsonRef, DefsRef]
|
JSON 引用到定义引用的映射。 |
definitions |
dict[DefsRef, JsonSchemaValue]
|
模式中的定义。 |
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
by_alias
|
bool
|
是否在生成的模式中使用字段别名。 |
True
|
ref_template
|
str
|
生成引用名称时使用的格式字符串。 |
DEFAULT_REF_TEMPLATE
|
引发
类型 | 描述 |
---|---|
JsonSchemaError
|
如果在生成模式后不小心重用了该类的实例。 |
源代码在 pydantic/json_schema.py
中
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
ValidationsMapping ¶
此类仅包含从 core_schema 属性名称到相应 JSON 模式属性名称的映射。 虽然我怀疑它不太可能是必要的,但原则上您可以在 GenerateJsonSchema 的子类中覆盖此类(通过从 GenerateJsonSchema.ValidationsMapping 继承)来更改这些映射。
build_schema_type_to_method ¶
build_schema_type_to_method() -> dict[
CoreSchemaOrFieldType,
Callable[[CoreSchemaOrField], JsonSchemaValue],
]
构建一个字典,将字段映射到用于生成 JSON 模式的方法。
返回
类型 | 描述 |
---|---|
dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]]
|
一个字典,包含 |
引发
类型 | 描述 |
---|---|
TypeError
|
如果未定义用于为给定 pydantic 核心模式类型生成 JSON 模式的方法。 |
源代码在 pydantic/json_schema.py
中
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
generate_definitions ¶
generate_definitions(
inputs: Sequence[
tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]
]
) -> tuple[
dict[
tuple[JsonSchemaKeyT, JsonSchemaMode],
JsonSchemaValue,
],
dict[DefsRef, JsonSchemaValue],
]
从核心模式列表中生成 JSON 模式定义,并将生成的定义与链接输入键到定义引用的映射配对。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
inputs
|
Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]]
|
元组序列,其中
|
required |
返回
类型 | 描述 |
---|---|
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]
|
一个元组,其中
|
引发
类型 | 描述 |
---|---|
PydanticUserError
|
如果 JSON 模式生成器已被用于生成 JSON 模式,则引发此错误。 |
源代码在 pydantic/json_schema.py
中
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
generate ¶
generate(
schema: CoreSchema, mode: JsonSchemaMode = "validation"
) -> JsonSchemaValue
为指定模式在指定模式下生成 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchema
|
Pydantic 模型。 |
required |
mode
|
JsonSchemaMode
|
生成模式的模式。 默认为“validation”。 |
'validation'
|
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
表示指定模式的 JSON 模式。 |
引发
类型 | 描述 |
---|---|
PydanticUserError
|
如果 JSON 模式生成器已被用于生成 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
generate_inner ¶
generate_inner(
schema: CoreSchemaOrField,
) -> JsonSchemaValue
为给定的核心模式生成 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchemaOrField
|
给定的核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
TODO:此处的嵌套函数定义似乎是不良做法,我希望在以后的 PR 中解包这些定义。 如果我们可以稍微缩短 JSON 模式生成的调用堆栈,那就太好了,我认为这里有潜力。
源代码在 pydantic/json_schema.py
中
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 |
|
sort ¶
sort(
value: JsonSchemaValue, parent_key: str | None = None
) -> JsonSchemaValue
覆盖此方法以自定义 JSON 模式的排序(例如,完全不排序,无条件排序所有键等)
默认情况下,按字母顺序对 JSON 模式中的键进行排序,跳过“properties”和“default”键以保留字段定义顺序。 此排序是递归的,因此它也将对所有嵌套字典进行排序。
源代码在 pydantic/json_schema.py
中
568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
invalid_schema ¶
invalid_schema(schema: InvalidSchema) -> JsonSchemaValue
占位符 - 永远不应调用。
源代码在 pydantic/json_schema.py
中
602 603 604 605 |
|
any_schema ¶
any_schema(schema: AnySchema) -> JsonSchemaValue
生成与任何值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
AnySchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
607 608 609 610 611 612 613 614 615 616 |
|
none_schema ¶
none_schema(schema: NoneSchema) -> JsonSchemaValue
生成与 None
匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
NoneSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
618 619 620 621 622 623 624 625 626 627 |
|
bool_schema ¶
bool_schema(schema: BoolSchema) -> JsonSchemaValue
生成与布尔值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BoolSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
629 630 631 632 633 634 635 636 637 638 |
|
int_schema ¶
int_schema(schema: IntSchema) -> JsonSchemaValue
生成与整数值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IntSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
640 641 642 643 644 645 646 647 648 649 650 651 652 |
|
float_schema ¶
float_schema(schema: FloatSchema) -> JsonSchemaValue
生成与浮点数值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
FloatSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
654 655 656 657 658 659 660 661 662 663 664 665 666 |
|
decimal_schema ¶
decimal_schema(schema: DecimalSchema) -> JsonSchemaValue
生成与十进制值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DecimalSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
668 669 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 |
|
str_schema ¶
str_schema(schema: StringSchema) -> JsonSchemaValue
生成与字符串值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
StringSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
|
bytes_schema ¶
bytes_schema(schema: BytesSchema) -> JsonSchemaValue
生成与字节值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BytesSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
717 718 719 720 721 722 723 724 725 726 727 728 |
|
date_schema ¶
date_schema(schema: DateSchema) -> JsonSchemaValue
生成与日期值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DateSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
730 731 732 733 734 735 736 737 738 739 |
|
time_schema ¶
time_schema(schema: TimeSchema) -> JsonSchemaValue
生成与时间值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TimeSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
741 742 743 744 745 746 747 748 749 750 |
|
datetime_schema ¶
datetime_schema(schema: DatetimeSchema) -> JsonSchemaValue
生成与日期时间值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DatetimeSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
752 753 754 755 756 757 758 759 760 761 |
|
timedelta_schema ¶
timedelta_schema(
schema: TimedeltaSchema,
) -> JsonSchemaValue
生成与时间差值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TimedeltaSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
763 764 765 766 767 768 769 770 771 772 773 774 |
|
literal_schema ¶
literal_schema(schema: LiteralSchema) -> JsonSchemaValue
生成与字面量值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
LiteralSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
776 777 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 |
|
enum_schema ¶
enum_schema(schema: EnumSchema) -> JsonSchemaValue
生成与 Enum 值匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
EnumSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
808 809 810 811 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 |
|
is_instance_schema ¶
is_instance_schema(
schema: IsInstanceSchema,
) -> JsonSchemaValue
处理核心模式的 JSON 模式生成,该核心模式检查值是否为类的实例。
除非在子类中被覆盖,否则会引发错误。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IsInstanceSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
844 845 846 847 848 849 850 851 852 853 854 855 |
|
is_subclass_schema ¶
is_subclass_schema(
schema: IsSubclassSchema,
) -> JsonSchemaValue
处理核心模式的 JSON 模式生成,该核心模式检查值是否为类的子类。
为了向后兼容 v1,这不会引发错误,但可以被覆盖以更改此行为。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IsSubclassSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
857 858 859 860 861 862 863 864 865 866 867 868 869 |
|
callable_schema ¶
callable_schema(schema: CallableSchema) -> JsonSchemaValue
生成与可调用值匹配的 JSON 模式。
除非在子类中被覆盖,否则会引发错误。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CallableSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
871 872 873 874 875 876 877 878 879 880 881 882 |
|
list_schema ¶
list_schema(schema: ListSchema) -> JsonSchemaValue
返回与列表模式匹配的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ListSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
884 885 886 887 888 889 890 891 892 893 894 895 896 |
|
tuple_positional_schema ¶
tuple_positional_schema(
schema: TupleSchema,
) -> JsonSchemaValue
已替换为 tuple_schema
。
源代码在 pydantic/json_schema.py
中
898 899 900 901 902 903 904 905 906 907 |
|
tuple_variable_schema ¶
tuple_variable_schema(
schema: TupleSchema,
) -> JsonSchemaValue
已替换为 tuple_schema
。
源代码在 pydantic/json_schema.py
中
909 910 911 912 913 914 915 916 917 918 |
|
tuple_schema ¶
tuple_schema(schema: TupleSchema) -> JsonSchemaValue
生成与元组模式匹配的 JSON 模式,例如 tuple[int, str, bool]
或 tuple[int, ...]
。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TupleSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
920 921 922 923 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 |
|
set_schema ¶
set_schema(schema: SetSchema) -> JsonSchemaValue
生成与集合模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
SetSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
955 956 957 958 959 960 961 962 963 964 |
|
frozenset_schema ¶
frozenset_schema(
schema: FrozenSetSchema,
) -> JsonSchemaValue
生成与 frozenset 模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
FrozenSetSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
966 967 968 969 970 971 972 973 974 975 |
|
generator_schema ¶
generator_schema(
schema: GeneratorSchema,
) -> JsonSchemaValue
返回表示提供的 GeneratorSchema 的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
GeneratorSchema
|
模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
983 984 985 986 987 988 989 990 991 992 993 994 995 |
|
dict_schema ¶
dict_schema(schema: DictSchema) -> JsonSchemaValue
生成与字典模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DictSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
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 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
|
function_before_schema ¶
function_before_schema(
schema: BeforeValidatorFunctionSchema,
) -> JsonSchemaValue
生成与 function-before 模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BeforeValidatorFunctionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
|
function_after_schema ¶
function_after_schema(
schema: AfterValidatorFunctionSchema,
) -> JsonSchemaValue
生成与 function-after 模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
AfterValidatorFunctionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
|
function_plain_schema ¶
function_plain_schema(
schema: PlainValidatorFunctionSchema,
) -> JsonSchemaValue
生成与 function-plain 模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
PlainValidatorFunctionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 |
|
function_wrap_schema ¶
function_wrap_schema(
schema: WrapValidatorFunctionSchema,
) -> JsonSchemaValue
生成与 function-wrap 模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WrapValidatorFunctionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 |
|
default_schema ¶
default_schema(
schema: WithDefaultSchema,
) -> JsonSchemaValue
生成与具有默认值的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WithDefaultSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
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 |
|
get_default_value ¶
get_default_value(schema: WithDefaultSchema) -> Any
获取在为具有默认值的核心模式生成 JSON Schema 时要使用的默认值。
默认实现是使用静态定义的默认值。 如果您想使用默认工厂,则可以覆盖此方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WithDefaultSchema
|
|
required |
返回
类型 | 描述 |
---|---|
Any
|
要使用的默认值,如果没有默认值,则为 |
源代码在 pydantic/json_schema.py
中
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 |
|
nullable_schema ¶
nullable_schema(schema: NullableSchema) -> JsonSchemaValue
生成与允许空值的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
NullableSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
|
union_schema ¶
union_schema(schema: UnionSchema) -> JsonSchemaValue
生成与允许与任何给定模式匹配的值的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UnionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
|
tagged_union_schema ¶
tagged_union_schema(
schema: TaggedUnionSchema,
) -> JsonSchemaValue
生成与允许与任何给定模式匹配的值的模式匹配的 JSON 模式,其中模式使用鉴别器字段标记,该字段指示应使用哪个模式来验证值。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TaggedUnionSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
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 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
|
chain_schema ¶
chain_schema(schema: ChainSchema) -> JsonSchemaValue
生成与 core_schema.ChainSchema 匹配的 JSON 模式。
在为验证生成模式时,我们返回链中第一步的验证 JSON 模式。 对于序列化,我们返回链中最后一步的序列化 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ChainSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(
schema: LaxOrStrictSchema,
) -> JsonSchemaValue
生成与允许与宽松模式或严格模式匹配的值的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
LaxOrStrictSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 |
|
json_or_python_schema ¶
json_or_python_schema(
schema: JsonOrPythonSchema,
) -> JsonSchemaValue
生成与允许与 JSON 模式或 Python 模式匹配的值的模式匹配的 JSON 模式。
JSON 模式代替 Python 模式使用。 如果要使用 Python 模式,则应覆盖此方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
JsonOrPythonSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 |
|
typed_dict_schema ¶
typed_dict_schema(
schema: TypedDictSchema,
) -> JsonSchemaValue
生成与定义类型化字典的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TypedDictSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(
schema: TypedDictField,
) -> JsonSchemaValue
生成与定义类型化字典字段的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TypedDictField
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 |
|
dataclass_field_schema ¶
dataclass_field_schema(
schema: DataclassField,
) -> JsonSchemaValue
生成与定义数据类字段的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassField
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 |
|
model_field_schema ¶
model_field_schema(schema: ModelField) -> JsonSchemaValue
生成与定义模型字段的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelField
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 |
|
computed_field_schema ¶
computed_field_schema(
schema: ComputedField,
) -> JsonSchemaValue
生成与定义计算字段的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ComputedField
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 |
|
model_schema ¶
model_schema(schema: ModelSchema) -> JsonSchemaValue
生成与定义模型的模式匹配的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 |
|
resolve_ref_schema ¶
resolve_ref_schema(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
如果 JsonSchemaValue 是 $ref 模式,则将其解析为非 ref 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
要解析的模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
解析后的模式。 |
引发
类型 | 描述 |
---|---|
RuntimeError
|
如果无法在定义中找到模式引用。 |
源代码在 pydantic/json_schema.py
中
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 |
|
model_fields_schema ¶
model_fields_schema(
schema: ModelFieldsSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义模型字段的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelFieldsSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 |
|
field_is_present ¶
field_is_present(field: CoreSchemaField) -> bool
字段是否应包含在生成的 JSON 模式中。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
CoreSchemaField
|
字段本身的模式。 |
required |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应包含在生成的 JSON 模式中,则为 |
源代码在 pydantic/json_schema.py
中
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 |
|
field_is_required ¶
字段是否应在生成的 JSON 模式中标记为必需。(请注意,如果字段未出现在 JSON 模式中,则这无关紧要)。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
ModelField | DataclassField | TypedDictField
|
字段本身的模式。 |
required |
total
|
bool
|
仅适用于 |
required |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应在生成的 JSON 模式中标记为必需,则为 |
源代码在 pydantic/json_schema.py
中
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 |
|
dataclass_args_schema ¶
dataclass_args_schema(
schema: DataclassArgsSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义数据类构造函数参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassArgsSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 |
|
dataclass_schema ¶
dataclass_schema(
schema: DataclassSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义数据类的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 |
|
arguments_schema ¶
arguments_schema(
schema: ArgumentsSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义函数参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ArgumentsSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 |
|
kw_arguments_schema ¶
kw_arguments_schema(
arguments: list[ArgumentsParameter],
var_kwargs_schema: CoreSchema | None,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义函数关键字参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
arguments
|
list[ArgumentsParameter]
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 |
|
p_arguments_schema ¶
p_arguments_schema(
arguments: list[ArgumentsParameter],
var_args_schema: CoreSchema | None,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义函数位置参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
arguments
|
list[ArgumentsParameter]
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
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 1809 1810 1811 1812 |
|
get_argument_name ¶
get_argument_name(
argument: ArgumentsParameter | ArgumentsV3Parameter,
) -> str
检索参数的名称。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
argument
|
ArgumentsParameter | ArgumentsV3Parameter
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
str
|
参数的名称。 |
源代码在 pydantic/json_schema.py
中
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 |
|
arguments_v3_schema ¶
arguments_v3_schema(
schema: ArgumentsV3Schema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义函数参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ArgumentsV3Schema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1832 1833 1834 1835 1836 1837 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 |
|
call_schema ¶
call_schema(schema: CallSchema) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义函数调用的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CallSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 |
|
custom_error_schema ¶
custom_error_schema(
schema: CustomErrorSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义自定义错误的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CustomErrorSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 |
|
json_schema ¶
json_schema(schema: JsonSchema) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义 JSON 对象的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
JsonSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 |
|
url_schema ¶
url_schema(schema: UrlSchema) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义 URL 的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UrlSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 |
|
multi_host_url_schema ¶
multi_host_url_schema(
schema: MultiHostUrlSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义可用于多个主机的 URL 的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
MultiHostUrlSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 |
|
uuid_schema ¶
uuid_schema(schema: UuidSchema) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配 UUID。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UuidSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 |
|
definitions_schema ¶
definitions_schema(
schema: DefinitionsSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配定义带有定义的 JSON 对象的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DefinitionsSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 |
|
definition_ref_schema ¶
definition_ref_schema(
schema: DefinitionReferenceSchema,
) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配引用定义的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DefinitionReferenceSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 |
|
ser_schema ¶
ser_schema(
schema: (
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
),
) -> JsonSchemaValue | None
生成一个 JSON 模式,该模式匹配定义序列化对象的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue | None
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 |
|
complex_schema ¶
complex_schema(schema: ComplexSchema) -> JsonSchemaValue
生成一个 JSON 模式,该模式匹配复数。
JSON 没有表示复数的标准方法。复数不是数字类型。这里我们将复数表示为字符串,遵循 Python 定义的规则。例如,“1+2j”是一个可接受的复数字符串。详情请参阅Python 的 complex
文档。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ComplexSchema
|
核心模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码在 pydantic/json_schema.py
中
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 |
|
get_title_from_name ¶
从名称中检索标题。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
name
|
str
|
要从中检索标题的名称。 |
required |
返回
类型 | 描述 |
---|---|
str
|
标题。 |
源代码在 pydantic/json_schema.py
中
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 |
|
field_title_should_be_set ¶
field_title_should_be_set(
schema: CoreSchemaOrField,
) -> bool
如果给定模式的字段应根据字段名称设置标题,则返回 true。
直观地,我们希望对于那些否则不会提供自己的标题的模式(例如,int、float、str)返回 true,而对于那些会提供标题的模式(例如,BaseModel 子类)返回 false。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchemaOrField
|
要检查的模式。 |
required |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应设置标题,则为 |
源代码在 pydantic/json_schema.py
中
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 |
|
normalize_name ¶
规范化名称以用作字典中的键。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
name
|
str
|
要规范化的名称。 |
required |
返回
类型 | 描述 |
---|---|
str
|
规范化的名称。 |
源代码在 pydantic/json_schema.py
中
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref: CoreModeRef) -> DefsRef
覆盖此方法以更改从核心引用生成定义键的方式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
core_mode_ref
|
CoreModeRef
|
核心引用。 |
required |
返回
类型 | 描述 |
---|---|
DefsRef
|
定义键。 |
源代码在 pydantic/json_schema.py
中
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 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 |
|
get_cache_defs_ref_schema ¶
get_cache_defs_ref_schema(
core_ref: CoreRef,
) -> tuple[DefsRef, JsonSchemaValue]
此方法使用一些缓存查找/填充逻辑包装 get_defs_ref 方法,并返回生成的 defs_ref 和将引用正确定义的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
core_ref
|
CoreRef
|
要获取定义引用的核心引用。 |
required |
返回
类型 | 描述 |
---|---|
tuple[DefsRef, JsonSchemaValue]
|
定义引用和将引用它的 JSON 模式的元组。 |
源代码在 pydantic/json_schema.py
中
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 |
|
handle_ref_overrides ¶
handle_ref_overrides(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
删除任何与引用模式冗余的同级键。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
要从中删除冗余同级键的模式。 |
required |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
删除了冗余同级键的模式。 |
源代码在 pydantic/json_schema.py
中
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 |
|
encode_default ¶
将默认值编码为 JSON 可序列化值。
这用于编码生成的 JSON 模式中字段的默认值。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
dft
|
Any
|
要编码的默认值。 |
required |
返回
类型 | 描述 |
---|---|
Any
|
编码后的默认值。 |
源代码在 pydantic/json_schema.py
中
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 |
|
update_with_validations ¶
update_with_validations(
json_schema: JsonSchemaValue,
core_schema: CoreSchema,
mapping: dict[str, str],
) -> None
使用提供的映射将 core_schema 中的键转换为 JSON 模式的相应键,从而使用 core_schema 中指定的相应验证来更新 json_schema。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
要更新的 JSON 模式。 |
required |
core_schema
|
CoreSchema
|
从中获取验证的核心模式。 |
required |
mapping
|
dict[str, str]
|
从 core_schema 属性名称到相应 JSON 模式属性名称的映射。 |
required |
源代码在 pydantic/json_schema.py
中
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 |
|
get_json_ref_counts ¶
get_json_ref_counts(
json_schema: JsonSchemaValue,
) -> dict[JsonRef, int]
获取 json_schema 中任何位置与键“$ref”对应的所有值。
源代码在 pydantic/json_schema.py
中
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 |
|
emit_warning ¶
emit_warning(
kind: JsonSchemaWarningKind, detail: str
) -> None
此方法仅根据 warning_message
方法中的处理发出 PydanticJsonSchemaWarnings。
源代码在 pydantic/json_schema.py
中
2320 2321 2322 2323 2324 |
|
render_warning_message ¶
render_warning_message(
kind: JsonSchemaWarningKind, detail: str
) -> str | None
此方法负责根据需要忽略警告,并格式化警告消息。
您可以在 GenerateJsonSchema 的子类中覆盖 ignored_warning_kinds
的值,以修改生成的警告。如果您想要更多控制,可以覆盖此方法;只需在您不希望发出警告的情况下返回 None。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
kind
|
JsonSchemaWarningKind
|
要呈现的警告类型。它可以是以下之一
|
required |
detail
|
str
|
包含有关警告的更多详细信息的字符串。 |
required |
返回
类型 | 描述 |
---|---|
str | None
|
格式化的警告消息,如果应不发出警告,则为 |
源代码在 pydantic/json_schema.py
中
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 |
|
WithJsonSchema dataclass
¶
WithJsonSchema(
json_schema: JsonSchemaValue | None,
mode: (
Literal["validation", "serialization"] | None
) = None,
)
使用文档
将此作为字段的注解添加,以覆盖将为该字段生成的(基本)JSON 模式。这提供了一种为在生成 JSON 模式时会引发错误的类型(例如 Callable)或具有 is-instance 核心模式的类型设置 JSON 模式的方法,而无需创建 pydantic.json_schema.GenerateJsonSchema 的自定义子类。请注意,通常会对模式进行的任何修改(例如,设置模型字段的标题)仍将执行。
如果设置了 mode
,则这将仅适用于该模式生成模式,允许您为验证和序列化设置不同的 json 模式。
Examples ¶
Examples(
examples: dict[str, Any] | list[Any],
mode: (
Literal["validation", "serialization"] | None
) = None,
)
向 JSON 模式添加示例。
如果 JSON 模式已包含示例,则将附加提供的示例。
如果设置了 mode
,则这将仅适用于该模式生成模式,允许您为验证和序列化添加不同的示例。
源代码在 pydantic/json_schema.py
中
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 |
|
SkipJsonSchema dataclass
¶
SkipJsonSchema()
使用文档
将此作为字段的注解添加,以跳过为该字段生成 JSON 模式。
Example
from pprint import pprint
from typing import Union
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema
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'
}
'''
- 整数和 null 类型都包含在
a
的模式中。 - 整数类型是
b
的模式中包含的唯一类型。 c
字段的全部内容都从模式中省略。
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]
用于为模型生成 JSON Schema 的实用函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
cls
|
type[BaseModel] | type[PydanticDataclass]
|
要为其生成 JSON Schema 的模型类。 |
required |
by_alias
|
bool
|
如果为 |
True
|
ref_template
|
str
|
用于生成 JSON Schema 引用的模板。 |
DEFAULT_REF_TEMPLATE
|
schema_generator
|
type[GenerateJsonSchema]
|
用于生成 JSON Schema 的类。 |
GenerateJsonSchema
|
mode
|
JsonSchemaMode
|
用于生成 JSON Schema 的模式。它可以是以下之一
|
'validation'
|
返回
类型 | 描述 |
---|---|
dict[str, Any]
|
生成的 JSON Schema。 |
源代码在 pydantic/json_schema.py
中
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 2408 2409 2410 2411 2412 2413 |
|
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,
]
用于为多个模型生成 JSON Schema 的实用函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
models
|
Sequence[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode]]
|
形式为 (模型, 模式) 的元组序列。 |
required |
by_alias
|
bool
|
是否应将字段别名用作生成的 JSON Schema 中的键。 |
True
|
title
|
str | None
|
生成的 JSON Schema 的标题。 |
None
|
description
|
str | None
|
生成的 JSON Schema 的描述。 |
None
|
ref_template
|
str
|
用于生成 JSON Schema 引用的引用模板。 |
DEFAULT_REF_TEMPLATE
|
schema_generator
|
type[GenerateJsonSchema]
|
用于生成 JSON Schema 的模式生成器。 |
GenerateJsonSchema
|
返回
类型 | 描述 |
---|---|
tuple[dict[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]
|
一个元组,其中:- 第一个元素是一个字典,其键是 JSON 模式键类型和 JSON 模式的元组,其值是与该输入对对应的 JSON 模式。(这些模式可能具有对第二个返回元素中定义的定义的 JsonRef 引用。) - 第二个元素是一个 JSON 模式,其中包含第一个返回元素中引用的所有定义,以及可选的标题和描述键。 |
源代码在 pydantic/json_schema.py
中
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 |
|