JSON Schema
使用文档
json_schema
模块包含类和函数,允许自定义生成 JSON Schema 的方式。
通常,您不需要直接使用此模块;相反,您可以使用 BaseModel.model_json_schema
和 TypeAdapter.json_schema
。
CoreSchemaOrFieldType 模块属性
¶
CoreSchemaOrFieldType = Literal[
CoreSchemaType, CoreSchemaFieldType
]
表示 core_schema.CoreSchemaType
和 core_schema.CoreSchemaFieldType
联合的已定义模式类型的类型别名。
JsonSchemaMode 模块属性
¶
JsonSchemaMode = Literal['validation', 'serialization']
表示 JSON 模式模式的类型别名;可以是 'validation'(验证)或 'serialization'(序列化)。
对于某些类型,验证的输入与序列化的输出不同。例如,计算字段只在序列化时存在,而不应在验证时提供。此标志提供了一种方法来指示您是需要验证输入所需的 JSON 模式,还是与序列化输出匹配的 JSON 模式。
JsonSchemaWarningKind 模块属性
¶
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,
union_format: Literal[
"any_of", "primitive_type_array"
] = "any_of",
)
使用文档
用于生成 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
|
union_format
|
Literal['any_of', 'primitive_type_array']
|
'any_of'
|
抛出
类型 | 描述 |
---|---|
JsonSchemaError
|
如果在生成模式后不小心重用了类的实例。 |
源代码位于 pydantic/json_schema.py
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
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
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 |
|
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]]
|
元组序列,其中:
|
必需 |
返回
类型 | 描述 |
---|---|
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]
|
一个元组,其中:
|
抛出
类型 | 描述 |
---|---|
PydanticUserError
|
如果 JSON 模式生成器已被用于生成 JSON 模式,则引发此错误。 |
源代码位于 pydantic/json_schema.py
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
generate ¶
generate(
schema: CoreSchema, mode: JsonSchemaMode = "validation"
) -> JsonSchemaValue
以指定模式为指定模式生成 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchema
|
一个 Pydantic 模型。 |
必需 |
mode
|
JsonSchemaMode
|
生成模式的模式。默认为 'validation'。 |
'validation'
|
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
表示指定模式的 JSON 模式。 |
抛出
类型 | 描述 |
---|---|
PydanticUserError
|
如果 JSON 模式生成器已被用于生成 JSON 模式。 |
源代码位于 pydantic/json_schema.py
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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
|
generate_inner ¶
generate_inner(
schema: CoreSchemaOrField,
) -> JsonSchemaValue
为给定的核心模式生成 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchemaOrField
|
给定的核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
TODO:这里的嵌套函数定义似乎是不好的做法,我希望在未来的 PR 中解开它们。如果我们可以稍微缩短 JSON 模式生成的调用堆栈,那就太好了,我认为这里有这种潜力。
源代码位于 pydantic/json_schema.py
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 569 570 571 572 573 574 575 576 577 578 579 580 581 |
|
sort ¶
sort(
value: JsonSchemaValue, parent_key: str | None = None
) -> JsonSchemaValue
重写此方法以自定义 JSON 模式的排序(例如,完全不排序,无条件排序所有键等)。
默认情况下,按字母顺序对 JSON 模式中的键进行排序,跳过 'properties' 和 'default' 键以保留字段定义顺序。此排序是递归的,因此它也将对所有嵌套字典进行排序。
源代码位于 pydantic/json_schema.py
583 584 585 586 587 588 589 590 591 592 593 594 595 |
|
invalid_schema ¶
invalid_schema(schema: InvalidSchema) -> JsonSchemaValue
占位符 - 永远不应调用。
源代码位于 pydantic/json_schema.py
615 616 617 618 |
|
any_schema ¶
any_schema(schema: AnySchema) -> JsonSchemaValue
生成匹配任何值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
AnySchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
620 621 622 623 624 625 626 627 628 629 |
|
none_schema ¶
none_schema(schema: NoneSchema) -> JsonSchemaValue
生成匹配 None
的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
NoneSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
631 632 633 634 635 636 637 638 639 640 |
|
bool_schema ¶
bool_schema(schema: BoolSchema) -> JsonSchemaValue
生成匹配布尔值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BoolSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
642 643 644 645 646 647 648 649 650 651 |
|
int_schema ¶
int_schema(schema: IntSchema) -> JsonSchemaValue
生成匹配整数值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IntSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
float_schema ¶
float_schema(schema: FloatSchema) -> JsonSchemaValue
生成匹配浮点值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
FloatSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
667 668 669 670 671 672 673 674 675 676 677 678 679 |
|
decimal_schema ¶
decimal_schema(schema: DecimalSchema) -> JsonSchemaValue
生成匹配十进制值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DecimalSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
|
str_schema ¶
str_schema(schema: StringSchema) -> JsonSchemaValue
生成匹配字符串值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
StringSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
|
bytes_schema ¶
bytes_schema(schema: BytesSchema) -> JsonSchemaValue
生成匹配字节值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BytesSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
772 773 774 775 776 777 778 779 780 781 782 783 |
|
date_schema ¶
date_schema(schema: DateSchema) -> JsonSchemaValue
生成匹配日期值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DateSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
785 786 787 788 789 790 791 792 793 794 |
|
time_schema ¶
time_schema(schema: TimeSchema) -> JsonSchemaValue
生成匹配时间值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TimeSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
796 797 798 799 800 801 802 803 804 805 |
|
datetime_schema ¶
datetime_schema(schema: DatetimeSchema) -> JsonSchemaValue
生成匹配日期时间值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DatetimeSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
807 808 809 810 811 812 813 814 815 816 |
|
timedelta_schema ¶
timedelta_schema(
schema: TimedeltaSchema,
) -> JsonSchemaValue
生成匹配 timedelta 值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TimedeltaSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
818 819 820 821 822 823 824 825 826 827 828 829 |
|
literal_schema ¶
literal_schema(schema: LiteralSchema) -> JsonSchemaValue
生成匹配字面值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
LiteralSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
missing_sentinel_schema ¶
missing_sentinel_schema(
schema: MissingSentinelSchema,
) -> JsonSchemaValue
生成匹配 MISSING
哨兵值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
MissingSentinelSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
863 864 865 866 867 868 869 870 871 872 |
|
enum_schema ¶
enum_schema(schema: EnumSchema) -> JsonSchemaValue
生成匹配枚举值的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
EnumSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 |
|
is_instance_schema ¶
is_instance_schema(
schema: IsInstanceSchema,
) -> JsonSchemaValue
处理用于检查值是否为类实例的核心模式的 JSON 模式生成。
除非在子类中重写,否则此操作会引发错误。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IsInstanceSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
910 911 912 913 914 915 916 917 918 919 920 921 |
|
is_subclass_schema ¶
is_subclass_schema(
schema: IsSubclassSchema,
) -> JsonSchemaValue
处理用于检查值是否为类子类的核心模式的 JSON 模式生成。
为了与 v1 兼容,这不会引发错误,但可以重写以更改此行为。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
IsSubclassSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
923 924 925 926 927 928 929 930 931 932 933 934 935 |
|
callable_schema ¶
callable_schema(schema: CallableSchema) -> JsonSchemaValue
生成匹配可调用值的 JSON 模式。
除非在子类中重写,否则此操作会引发错误。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CallableSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
937 938 939 940 941 942 943 944 945 946 947 948 |
|
list_schema ¶
list_schema(schema: ListSchema) -> JsonSchemaValue
返回匹配列表模式的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ListSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
950 951 952 953 954 955 956 957 958 959 960 961 962 |
|
tuple_positional_schema ¶
tuple_positional_schema(
schema: TupleSchema,
) -> JsonSchemaValue
已被 tuple_schema
替换。
源代码位于 pydantic/json_schema.py
964 965 966 967 968 969 970 971 972 973 |
|
tuple_variable_schema ¶
tuple_variable_schema(
schema: TupleSchema,
) -> JsonSchemaValue
已被 tuple_schema
替换。
源代码位于 pydantic/json_schema.py
975 976 977 978 979 980 981 982 983 984 |
|
tuple_schema ¶
tuple_schema(schema: TupleSchema) -> JsonSchemaValue
生成匹配元组模式的 JSON 模式,例如 tuple[int, str, bool]
或 tuple[int, ...]
。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TupleSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
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 |
|
set_schema ¶
set_schema(schema: SetSchema) -> JsonSchemaValue
生成匹配集合模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
SetSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
|
frozenset_schema ¶
frozenset_schema(
schema: FrozenSetSchema,
) -> JsonSchemaValue
生成匹配 frozenset 模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
FrozenSetSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
|
generator_schema ¶
generator_schema(
schema: GeneratorSchema,
) -> JsonSchemaValue
返回表示所提供 GeneratorSchema 的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
GeneratorSchema
|
模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 |
|
dict_schema ¶
dict_schema(schema: DictSchema) -> JsonSchemaValue
生成匹配字典模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DictSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 |
|
function_before_schema ¶
function_before_schema(
schema: BeforeValidatorFunctionSchema,
) -> JsonSchemaValue
生成匹配函数前置模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
BeforeValidatorFunctionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
|
function_after_schema ¶
function_after_schema(
schema: AfterValidatorFunctionSchema,
) -> JsonSchemaValue
生成匹配函数后置模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
AfterValidatorFunctionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
|
function_plain_schema ¶
function_plain_schema(
schema: PlainValidatorFunctionSchema,
) -> JsonSchemaValue
生成匹配函数纯模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
PlainValidatorFunctionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 |
|
function_wrap_schema ¶
function_wrap_schema(
schema: WrapValidatorFunctionSchema,
) -> JsonSchemaValue
生成匹配函数包装模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WrapValidatorFunctionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
|
default_schema ¶
default_schema(
schema: WithDefaultSchema,
) -> JsonSchemaValue
生成匹配具有默认值的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WithDefaultSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
|
get_default_value ¶
get_default_value(schema: WithDefaultSchema) -> Any
获取在为具有默认值的核心模式生成 JSON Schema 时使用的默认值。
默认实现是使用静态定义的默认值。如果想使用默认工厂,可以重写此方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
WithDefaultSchema
|
|
必需 |
返回
类型 | 描述 |
---|---|
Any
|
要使用的默认值,如果没有可用的默认值,则为 |
源代码位于 pydantic/json_schema.py
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
|
nullable_schema ¶
nullable_schema(schema: NullableSchema) -> JsonSchemaValue
生成匹配允许空值的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
NullableSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
|
union_schema ¶
union_schema(schema: UnionSchema) -> JsonSchemaValue
生成匹配允许值与给定任何模式匹配的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UnionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 |
|
get_union_of_schemas ¶
get_union_of_schemas(
schemas: list[JsonSchemaValue],
) -> JsonSchemaValue
返回所提供的 JSON Schema 联合的 JSON Schema 表示。
结果取决于配置的 'union_format'
。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schemas
|
列表[JsonSchemaValue]
|
要包含在联合中的 JSON Schema 列表。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
表示模式联合的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 |
|
tagged_union_schema ¶
tagged_union_schema(
schema: TaggedUnionSchema,
) -> JsonSchemaValue
生成匹配模式的 JSON 模式,该模式允许值与给定模式中的任何一个匹配,其中模式用指示应使用哪个模式验证值的鉴别器字段进行标记。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TaggedUnionSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
|
chain_schema ¶
chain_schema(schema: ChainSchema) -> JsonSchemaValue
生成匹配 core_schema.ChainSchema 的 JSON 模式。
为验证生成模式时,我们返回链中第一步的验证 JSON 模式。对于序列化,我们返回链中最后一步的序列化 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ChainSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(
schema: LaxOrStrictSchema,
) -> JsonSchemaValue
生成匹配允许值与宽松模式或严格模式匹配的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
LaxOrStrictSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 |
|
json_or_python_schema ¶
json_or_python_schema(
schema: JsonOrPythonSchema,
) -> JsonSchemaValue
生成匹配允许值与 JSON 模式或 Python 模式匹配的模式的 JSON 模式。
使用 JSON 模式而不是 Python 模式。如果要使用 Python 模式,则应重写此方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
JsonOrPythonSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 |
|
typed_dict_schema ¶
typed_dict_schema(
schema: TypedDictSchema,
) -> JsonSchemaValue
生成匹配定义 TypedDict 的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TypedDictSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1444 1445 1446 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 1485 1486 1487 1488 1489 1490 1491 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(
schema: TypedDictField,
) -> JsonSchemaValue
生成匹配定义 TypedDict 字段的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
TypedDictField
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 |
|
dataclass_field_schema ¶
dataclass_field_schema(
schema: DataclassField,
) -> JsonSchemaValue
生成匹配定义数据类字段的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassField
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 |
|
model_field_schema ¶
model_field_schema(schema: ModelField) -> JsonSchemaValue
生成匹配定义模型字段的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelField
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 |
|
computed_field_schema ¶
computed_field_schema(
schema: ComputedField,
) -> JsonSchemaValue
生成匹配定义计算字段的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ComputedField
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
|
model_schema ¶
model_schema(schema: ModelSchema) -> JsonSchemaValue
生成匹配定义模型的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 |
|
resolve_ref_schema ¶
resolve_ref_schema(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
如果 JsonSchemaValue 是 $ref 模式,则将其解析为非引用模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
要解析的模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
解析后的模式。 |
抛出
类型 | 描述 |
---|---|
RuntimeError
|
如果在定义中找不到模式引用。 |
源代码位于 pydantic/json_schema.py
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 |
|
model_fields_schema ¶
model_fields_schema(
schema: ModelFieldsSchema,
) -> JsonSchemaValue
生成匹配定义模型字段的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ModelFieldsSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 |
|
field_is_present ¶
field_is_present(field: CoreSchemaField) -> bool
字段是否应包含在生成的 JSON 模式中。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
CoreSchemaField
|
字段本身的模式。 |
必需 |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应包含在生成的 JSON 模式中,则为 |
源代码位于 pydantic/json_schema.py
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 |
|
field_is_required ¶
字段是否应在生成的 JSON 模式中标记为必需。(请注意,如果字段不存在于 JSON 模式中,则此项无关紧要。)
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
ModelField | DataclassField | TypedDictField
|
字段本身的模式。 |
必需 |
total
|
bool
|
仅适用于 |
必需 |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应在生成的 JSON 模式中标记为必需,则为 |
源代码位于 pydantic/json_schema.py
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 |
|
dataclass_args_schema ¶
dataclass_args_schema(
schema: DataclassArgsSchema,
) -> JsonSchemaValue
生成匹配定义数据类构造函数参数的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassArgsSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 |
|
dataclass_schema ¶
dataclass_schema(
schema: DataclassSchema,
) -> JsonSchemaValue
生成匹配定义数据类的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DataclassSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
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 1813 |
|
arguments_schema ¶
arguments_schema(
schema: ArgumentsSchema,
) -> JsonSchemaValue
生成匹配定义函数参数的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ArgumentsSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 |
|
kw_arguments_schema ¶
kw_arguments_schema(
arguments: list[ArgumentsParameter],
var_kwargs_schema: CoreSchema | None,
) -> JsonSchemaValue
生成匹配定义函数关键字参数的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
arguments
|
列表[ArgumentsParameter]
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
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 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 |
|
p_arguments_schema ¶
p_arguments_schema(
arguments: list[ArgumentsParameter],
var_args_schema: CoreSchema | None,
) -> JsonSchemaValue
生成匹配定义函数位置参数的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
arguments
|
列表[ArgumentsParameter]
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 |
|
get_argument_name ¶
get_argument_name(
argument: ArgumentsParameter | ArgumentsV3Parameter,
) -> str
检索参数的名称。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
argument
|
ArgumentsParameter | ArgumentsV3Parameter
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
str
|
参数的名称。 |
源代码位于 pydantic/json_schema.py
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 |
|
arguments_v3_schema ¶
arguments_v3_schema(
schema: ArgumentsV3Schema,
) -> JsonSchemaValue
生成匹配定义函数参数的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ArgumentsV3Schema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 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 |
|
call_schema ¶
call_schema(schema: CallSchema) -> JsonSchemaValue
生成匹配定义函数调用的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CallSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 |
|
custom_error_schema ¶
custom_error_schema(
schema: CustomErrorSchema,
) -> JsonSchemaValue
生成匹配定义自定义错误的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CustomErrorSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 |
|
json_schema ¶
json_schema(schema: JsonSchema) -> JsonSchemaValue
生成匹配定义 JSON 对象的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
JsonSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 |
|
url_schema ¶
url_schema(schema: UrlSchema) -> JsonSchemaValue
生成匹配定义 URL 的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UrlSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 |
|
multi_host_url_schema ¶
multi_host_url_schema(
schema: MultiHostUrlSchema,
) -> JsonSchemaValue
生成匹配定义可在多个主机上使用的 URL 的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
MultiHostUrlSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 |
|
uuid_schema ¶
uuid_schema(schema: UuidSchema) -> JsonSchemaValue
生成匹配 UUID 的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
UuidSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 |
|
definitions_schema ¶
definitions_schema(
schema: DefinitionsSchema,
) -> JsonSchemaValue
生成匹配定义带有定义的 JSON 对象的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DefinitionsSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 |
|
definition_ref_schema ¶
definition_ref_schema(
schema: DefinitionReferenceSchema,
) -> JsonSchemaValue
生成匹配引用定义的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
DefinitionReferenceSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 |
|
ser_schema ¶
ser_schema(
schema: (
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
),
) -> JsonSchemaValue | None
生成匹配定义序列化对象的模式的 JSON 模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue | 无
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 |
|
complex_schema ¶
complex_schema(schema: ComplexSchema) -> JsonSchemaValue
生成匹配复数的 JSON 模式。
JSON 没有表示复数的标准方法。复数不是数字类型。在这里,我们按照 Python 定义的规则将复数表示为字符串。例如,'1+2j' 是一个可接受的复数字符串。详细信息可在 Python 的 complex
文档中找到。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
ComplexSchema
|
核心模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
生成的 JSON 模式。 |
源代码位于 pydantic/json_schema.py
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 |
|
get_title_from_name ¶
从名称中检索标题。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
name
|
str
|
要从中检索标题的名称。 |
必需 |
返回
类型 | 描述 |
---|---|
str
|
标题。 |
源代码位于 pydantic/json_schema.py
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 |
|
field_title_should_be_set ¶
field_title_should_be_set(
schema: CoreSchemaOrField,
) -> bool
如果具有给定模式的字段应根据字段名称设置标题,则返回 true。
直观地,我们希望这对于不会自行提供标题的模式(例如 int、float、str)返回 true,对于会提供标题的模式(例如 BaseModel 子类)返回 false。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
schema
|
CoreSchemaOrField
|
要检查的模式。 |
必需 |
返回
类型 | 描述 |
---|---|
bool
|
如果字段应设置标题,则为 |
源代码位于 pydantic/json_schema.py
2151 2152 2153 2154 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 2181 2182 2183 2184 |
|
normalize_name ¶
规范化名称以用作字典中的键。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
name
|
str
|
要规范化的名称。 |
必需 |
返回
类型 | 描述 |
---|---|
str
|
规范化后的名称。 |
源代码位于 pydantic/json_schema.py
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref: CoreModeRef) -> DefsRef
重写此方法以更改从核心引用生成定义键的方式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
core_mode_ref
|
CoreModeRef
|
核心引用。 |
必需 |
返回
类型 | 描述 |
---|---|
DefsRef
|
定义键。 |
源代码位于 pydantic/json_schema.py
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 |
|
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
|
要获取定义引用的核心引用。 |
必需 |
返回
类型 | 描述 |
---|---|
tuple[DefsRef, JsonSchemaValue]
|
定义引用和将引用它的 JSON 模式的元组。 |
源代码位于 pydantic/json_schema.py
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 |
|
handle_ref_overrides ¶
handle_ref_overrides(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
删除与引用模式重复的任何同级键。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
要从中删除冗余同级键的模式。 |
必需 |
返回
类型 | 描述 |
---|---|
JsonSchemaValue
|
已删除冗余同级键的模式。 |
源代码位于 pydantic/json_schema.py
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 |
|
encode_default ¶
将默认值编码为可 JSON 序列化的值。
这用于编码生成的 JSON 模式中字段的默认值。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
dft
|
Any
|
要编码的默认值。 |
必需 |
返回
类型 | 描述 |
---|---|
Any
|
编码后的默认值。 |
源代码位于 pydantic/json_schema.py
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 |
|
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 模式。 |
必需 |
core_schema
|
CoreSchema
|
要从中获取验证的核心模式。 |
必需 |
mapping
|
dict[str, str]
|
从 core_schema 属性名称到相应 JSON 模式属性名称的映射。 |
必需 |
源代码位于 pydantic/json_schema.py
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 |
|
get_json_ref_counts ¶
get_json_ref_counts(
json_schema: JsonSchemaValue,
) -> dict[JsonRef, int]
获取 json_schema 中任何地方与键 '$ref' 对应的所有值。
源代码位于 pydantic/json_schema.py
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 |
|
emit_warning ¶
emit_warning(
kind: JsonSchemaWarningKind, detail: str
) -> None
此方法仅根据 warning_message
方法中的处理发出 PydanticJsonSchemaWarnings。
源代码位于 pydantic/json_schema.py
2438 2439 2440 2441 2442 |
|
render_warning_message ¶
render_warning_message(
kind: JsonSchemaWarningKind, detail: str
) -> str | None
此方法负责根据需要忽略警告并格式化警告消息。
您可以在 GenerateJsonSchema 的子类中覆盖 ignored_warning_kinds
的值,以修改生成哪些警告。如果需要更多控制,可以重写此方法;只需在不想发出警告的情况下返回 None 即可。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
kind
|
JsonSchemaWarningKind
|
要渲染的警告类型。它可以是以下之一:
|
必需 |
detail
|
str
|
包含警告附加详细信息的字符串。 |
必需 |
返回
类型 | 描述 |
---|---|
str | None
|
格式化的警告消息,如果不需要发出警告,则为 |
源代码位于 pydantic/json_schema.py
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 |
|
WithJsonSchema 数据类
¶
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 Schema 已经包含示例,则提供的示例将被追加。
如果设置了 mode
,则仅适用于该模式生成模式,允许您为验证和序列化添加不同的示例。
源代码位于 pydantic/json_schema.py
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 |
|
SkipJsonSchema 数据类
¶
SkipJsonSchema()
使用文档
将其作为字段上的注释添加,以跳过为该字段生成 JSON 模式。
示例
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,
union_format: Literal[
"any_of", "primitive_type_array"
] = "any_of",
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema,
mode: JsonSchemaMode = "validation",
) -> dict[str, Any]
用于为模型生成 JSON Schema 的实用函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
cls
|
类型[BaseModel] | 类型[PydanticDataclass]
|
要生成 JSON Schema 的模型类。 |
必需 |
by_alias
|
bool
|
如果为 |
True
|
ref_template
|
str
|
用于生成 JSON Schema 引用的模板。 |
DEFAULT_REF_TEMPLATE
|
union_format
|
Literal['any_of', 'primitive_type_array']
|
'any_of'
|
|
schema_generator
|
类型[GenerateJsonSchema]
|
用于生成 JSON Schema 的类。 |
GenerateJsonSchema
|
mode
|
JsonSchemaMode
|
用于生成 JSON Schema 的模式。它可以是以下之一:
|
'validation'
|
返回
类型 | 描述 |
---|---|
dict[Any, Any]
|
生成的 JSON Schema。 |
源代码位于 pydantic/json_schema.py
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 |
|
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,
union_format: Literal[
"any_of", "primitive_type_array"
] = "any_of",
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema
) -> tuple[
dict[
tuple[
type[BaseModel] | type[PydanticDataclass],
JsonSchemaMode,
],
JsonSchemaValue,
],
JsonSchemaValue,
]
用于为多个模型生成 JSON Schema 的实用函数。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
models
|
序列[元组[类型[BaseModel] | 类型[PydanticDataclass], JsonSchemaMode]]
|
形式为 (model, mode) 的元组序列。 |
必需 |
by_alias
|
bool
|
字段别名是否应在生成的 JSON Schema 中用作键。 |
True
|
title
|
str | None
|
生成的 JSON Schema 的标题。 |
None
|
描述
|
str | None
|
生成的 JSON Schema 的描述。 |
None
|
ref_template
|
str
|
用于生成 JSON Schema 引用的引用模板。 |
DEFAULT_REF_TEMPLATE
|
union_format
|
Literal['any_of', 'primitive_type_array']
|
'any_of'
|
|
schema_generator
|
类型[GenerateJsonSchema]
|
用于生成 JSON Schema 的模式生成器。 |
GenerateJsonSchema
|
返回
类型 | 描述 |
---|---|
tuple[dict[tuple[类型[BaseModel] | 类型[PydanticDataclass], JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]
|
一个元组,其中: - 第一个元素是一个字典,其键是 JSON 模式键类型和 JSON 模式的元组,其值是与该输入对对应的 JSON 模式。(这些模式可能包含对第二个返回元素中定义的定义的 JsonRef 引用。) - 第二个元素是一个 JSON 模式,其中包含第一个返回元素中引用的所有定义,以及可选的标题和描述键。 |
源代码位于 pydantic/json_schema.py
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 |
|