pydantic_core.core_schema
This module contains definitions to build schemas which pydantic_core
can
validate and serialize.
WhenUsed
module-attribute
¶
WhenUsed = Literal[
"always", "unless-none", "json", "json-unless-none"
]
Values have the following meanings:
'always'
means always use'unless-none'
means use unless the value isNone
'json'
means use when serializing to JSON'json-unless-none'
means use when serializing to JSON and the value is notNone
CoreConfig ¶
Bases: TypedDict
Base class for schema configuration options.
Attributes:
Name | Type | Description |
---|---|---|
title |
str
|
The name of the configuration. |
strict |
bool
|
Whether the configuration should strictly adhere to specified rules. |
extra_fields_behavior |
ExtraBehavior
|
The behavior for handling extra fields. |
typed_dict_total |
bool
|
Whether the TypedDict should be considered total. Default is |
from_attributes |
bool
|
Whether to use attributes for models, dataclasses, and tagged union keys. |
loc_by_alias |
bool
|
Whether to use the used alias (or first alias for "field required" errors) instead of
|
revalidate_instances |
Literal['always', 'never', 'subclass-instances']
|
Whether instances of models and dataclasses should re-validate. Default is 'never'. |
validate_default |
bool
|
Whether to validate default values during validation. Default is |
populate_by_name |
bool
|
Whether an aliased field may be populated by its name as given by the model attribute,
as well as the alias. (Replaces 'allow_population_by_field_name' in Pydantic v1.) Default is |
str_max_length |
int
|
The maximum length for string fields. |
str_min_length |
int
|
The minimum length for string fields. |
str_strip_whitespace |
bool
|
Whether to strip whitespace from string fields. |
str_to_lower |
bool
|
Whether to convert string fields to lowercase. |
str_to_upper |
bool
|
Whether to convert string fields to uppercase. |
allow_inf_nan |
bool
|
Whether to allow infinity and NaN values for float fields. Default is |
ser_json_timedelta |
Literal['iso8601', 'float']
|
The serialization option for |
ser_json_bytes |
Literal['utf8', 'base64', 'hex']
|
The serialization option for |
ser_json_inf_nan |
Literal['null', 'constants']
|
The serialization option for infinity and NaN values in float fields. Default is 'null'. |
hide_input_in_errors |
bool
|
Whether to hide input data from |
validation_error_cause |
bool
|
Whether to add user-python excs to the cause of a ValidationError. Requires exceptiongroup backport pre Python 3.11. |
coerce_numbers_to_str |
bool
|
Whether to enable coercion of any |
regex_engine |
Literal['rust-regex', 'python-re']
|
The regex engine to use for regex pattern validation. Default is 'rust-regex'. See |
cache_strings |
Union[bool, Literal['all', 'keys', 'none']]
|
Whether to cache strings. Default is |
SerializationInfo ¶
simple_ser_schema ¶
simple_ser_schema(
type: ExpectedSerializationTypes,
) -> SimpleSerSchema
Returns a schema for serialization with a custom type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type |
ExpectedSerializationTypes
|
The type to use for serialization |
required |
Source code in pydantic_core/core_schema.py
227 228 229 230 231 232 233 234 |
|
plain_serializer_function_ser_schema ¶
plain_serializer_function_ser_schema(
function: SerializerFunction,
*,
is_field_serializer: bool | None = None,
info_arg: bool | None = None,
return_schema: CoreSchema | None = None,
when_used: WhenUsed = "always"
) -> PlainSerializerFunctionSerSchema
Returns a schema for serialization with a function, can be either a "general" or "field" function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
SerializerFunction
|
The function to use for serialization |
required |
is_field_serializer |
bool | None
|
Whether the serializer is for a field, e.g. takes |
None
|
info_arg |
bool | None
|
Whether the function takes an |
None
|
return_schema |
CoreSchema | None
|
Schema to use for serializing return value |
None
|
when_used |
WhenUsed
|
When the function should be called |
'always'
|
Source code in pydantic_core/core_schema.py
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 |
|
wrap_serializer_function_ser_schema ¶
wrap_serializer_function_ser_schema(
function: WrapSerializerFunction,
*,
is_field_serializer: bool | None = None,
info_arg: bool | None = None,
schema: CoreSchema | None = None,
return_schema: CoreSchema | None = None,
when_used: WhenUsed = "always"
) -> WrapSerializerFunctionSerSchema
Returns a schema for serialization with a wrap function, can be either a "general" or "field" function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
WrapSerializerFunction
|
The function to use for serialization |
required |
is_field_serializer |
bool | None
|
Whether the serializer is for a field, e.g. takes |
None
|
info_arg |
bool | None
|
Whether the function takes an |
None
|
schema |
CoreSchema | None
|
The schema to use for the inner serialization |
None
|
return_schema |
CoreSchema | None
|
Schema to use for serializing return value |
None
|
when_used |
WhenUsed
|
When the function should be called |
'always'
|
Source code in pydantic_core/core_schema.py
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 |
|
format_ser_schema ¶
format_ser_schema(
formatting_string: str,
*,
when_used: WhenUsed = "json-unless-none"
) -> FormatSerSchema
Returns a schema for serialization using python's format
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatting_string |
str
|
String defining the format to use |
required |
when_used |
WhenUsed
|
Same meaning as for [general_function_plain_ser_schema], but with a different default |
'json-unless-none'
|
Source code in pydantic_core/core_schema.py
375 376 377 378 379 380 381 382 383 384 385 386 |
|
to_string_ser_schema ¶
to_string_ser_schema(
*, when_used: WhenUsed = "json-unless-none"
) -> ToStringSerSchema
Returns a schema for serialization using python's str()
/ __str__
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
when_used |
WhenUsed
|
Same meaning as for [general_function_plain_ser_schema], but with a different default |
'json-unless-none'
|
Source code in pydantic_core/core_schema.py
394 395 396 397 398 399 400 401 402 403 404 405 |
|
model_ser_schema ¶
Returns a schema for serialization using a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type[Any]
|
The expected class type, used to generate warnings if the wrong type is passed |
required |
schema |
CoreSchema
|
Internal schema to use to serialize the model dict |
required |
Source code in pydantic_core/core_schema.py
414 415 416 417 418 419 420 421 422 |
|
computed_field ¶
computed_field(
property_name: str,
return_schema: CoreSchema,
*,
alias: str | None = None,
metadata: Any = None
) -> ComputedField
ComputedFields are properties of a model or dataclass that are included in serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property_name |
str
|
The name of the property on the model or dataclass |
required |
return_schema |
CoreSchema
|
The schema used for the type returned by the computed field |
required |
alias |
str | None
|
The name to use in the serialized output |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
Source code in pydantic_core/core_schema.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
any_schema ¶
any_schema(
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> AnySchema
Returns a schema that matches any value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.any_schema()
v = SchemaValidator(schema)
assert v.validate_python(1) == 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
|
none_schema ¶
none_schema(
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> NoneSchema
Returns a schema that matches a None value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.none_schema()
v = SchemaValidator(schema)
assert v.validate_python(None) is None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
|
bool_schema ¶
bool_schema(
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None,
) -> BoolSchema
Returns a schema that matches a bool value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.bool_schema()
v = SchemaValidator(schema)
assert v.validate_python('True') is True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
Whether the value should be a bool or a value that can be converted to a bool |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
|
int_schema ¶
int_schema(
*,
multiple_of: int | None = None,
le: int | None = None,
ge: int | None = None,
lt: int | None = None,
gt: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> IntSchema
Returns a schema that matches a int value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.int_schema(multiple_of=2, le=6, ge=2)
v = SchemaValidator(schema)
assert v.validate_python('4') == 4
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multiple_of |
int | None
|
The value must be a multiple of this number |
None
|
le |
int | None
|
The value must be less than or equal to this number |
None
|
ge |
int | None
|
The value must be greater than or equal to this number |
None
|
lt |
int | None
|
The value must be strictly less than this number |
None
|
gt |
int | None
|
The value must be strictly greater than this number |
None
|
strict |
bool | None
|
Whether the value should be a int or a value that can be converted to a int |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
float_schema ¶
float_schema(
*,
allow_inf_nan: bool | None = None,
multiple_of: float | None = None,
le: float | None = None,
ge: float | None = None,
lt: float | None = None,
gt: float | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> FloatSchema
Returns a schema that matches a float value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.float_schema(le=0.8, ge=0.2)
v = SchemaValidator(schema)
assert v.validate_python('0.5') == 0.5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_inf_nan |
bool | None
|
Whether to allow inf and nan values |
None
|
multiple_of |
float | None
|
The value must be a multiple of this number |
None
|
le |
float | None
|
The value must be less than or equal to this number |
None
|
ge |
float | None
|
The value must be greater than or equal to this number |
None
|
lt |
float | None
|
The value must be strictly less than this number |
None
|
gt |
float | None
|
The value must be strictly greater than this number |
None
|
strict |
bool | None
|
Whether the value should be a float or a value that can be converted to a float |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
decimal_schema ¶
decimal_schema(
*,
allow_inf_nan: bool = None,
multiple_of: Decimal | None = None,
le: Decimal | None = None,
ge: Decimal | None = None,
lt: Decimal | None = None,
gt: Decimal | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> DecimalSchema
Returns a schema that matches a decimal value, e.g.:
from decimal import Decimal
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.decimal_schema(le=0.8, ge=0.2)
v = SchemaValidator(schema)
assert v.validate_python('0.5') == Decimal('0.5')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_inf_nan |
bool
|
Whether to allow inf and nan values |
None
|
multiple_of |
Decimal | None
|
The value must be a multiple of this number |
None
|
le |
Decimal | None
|
The value must be less than or equal to this number |
None
|
ge |
Decimal | None
|
The value must be greater than or equal to this number |
None
|
lt |
Decimal | None
|
The value must be strictly less than this number |
None
|
gt |
Decimal | None
|
The value must be strictly greater than this number |
None
|
max_digits |
int | None
|
The maximum number of decimal digits allowed |
None
|
decimal_places |
int | None
|
The maximum number of decimal places allowed |
None
|
strict |
bool | None
|
Whether the value should be a float or a value that can be converted to a float |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 |
|
str_schema ¶
str_schema(
*,
pattern: str | None = None,
max_length: int | None = None,
min_length: int | None = None,
strip_whitespace: bool | None = None,
to_lower: bool | None = None,
to_upper: bool | None = None,
regex_engine: Literal["rust-regex", "python-re"]
| None = None,
strict: bool | None = None,
coerce_numbers_to_str: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> StringSchema
Returns a schema that matches a string value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.str_schema(max_length=10, min_length=2)
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern |
str | None
|
A regex pattern that the value must match |
None
|
max_length |
int | None
|
The value must be at most this length |
None
|
min_length |
int | None
|
The value must be at least this length |
None
|
strip_whitespace |
bool | None
|
Whether to strip whitespace from the value |
None
|
to_lower |
bool | None
|
Whether to convert the value to lowercase |
None
|
to_upper |
bool | None
|
Whether to convert the value to uppercase |
None
|
regex_engine |
Literal['rust-regex', 'python-re'] | None
|
None
|
|
strict |
bool | None
|
Whether the value should be a string or a value that can be converted to a string |
None
|
coerce_numbers_to_str |
bool | None
|
Whether to enable coercion of any |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 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 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
|
bytes_schema ¶
bytes_schema(
*,
max_length: int | None = None,
min_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> BytesSchema
Returns a schema that matches a bytes value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.bytes_schema(max_length=10, min_length=2)
v = SchemaValidator(schema)
assert v.validate_python(b'hello') == b'hello'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_length |
int | None
|
The value must be at most this length |
None
|
min_length |
int | None
|
The value must be at least this length |
None
|
strict |
bool | None
|
Whether the value should be a bytes or a value that can be converted to a bytes |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 862 863 864 865 866 867 868 869 |
|
date_schema ¶
date_schema(
*,
strict: bool | None = None,
le: date | None = None,
ge: date | None = None,
lt: date | None = None,
gt: date | None = None,
now_op: Literal["past", "future"] | None = None,
now_utc_offset: int | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> DateSchema
Returns a schema that matches a date value, e.g.:
from datetime import date
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.date_schema(le=date(2020, 1, 1), ge=date(2019, 1, 1))
v = SchemaValidator(schema)
assert v.validate_python(date(2019, 6, 1)) == date(2019, 6, 1)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
Whether the value should be a date or a value that can be converted to a date |
None
|
le |
date | None
|
The value must be less than or equal to this date |
None
|
ge |
date | None
|
The value must be greater than or equal to this date |
None
|
lt |
date | None
|
The value must be strictly less than this date |
None
|
gt |
date | None
|
The value must be strictly greater than this date |
None
|
now_op |
Literal['past', 'future'] | None
|
The value must be in the past or future relative to the current date |
None
|
now_utc_offset |
int | None
|
The value must be in the past or future relative to the current date with this utc offset |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
|
time_schema ¶
time_schema(
*,
strict: bool | None = None,
le: time | None = None,
ge: time | None = None,
lt: time | None = None,
gt: time | None = None,
tz_constraint: Literal["aware", "naive"]
| int
| None = None,
microseconds_precision: Literal[
"truncate", "error"
] = "truncate",
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> TimeSchema
Returns a schema that matches a time value, e.g.:
from datetime import time
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.time_schema(le=time(12, 0, 0), ge=time(6, 0, 0))
v = SchemaValidator(schema)
assert v.validate_python(time(9, 0, 0)) == time(9, 0, 0)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
Whether the value should be a time or a value that can be converted to a time |
None
|
le |
time | None
|
The value must be less than or equal to this time |
None
|
ge |
time | None
|
The value must be greater than or equal to this time |
None
|
lt |
time | None
|
The value must be strictly less than this time |
None
|
gt |
time | None
|
The value must be strictly greater than this time |
None
|
tz_constraint |
Literal['aware', 'naive'] | int | None
|
The value must be timezone aware or naive, or an int to indicate required tz offset |
None
|
microseconds_precision |
Literal['truncate', 'error']
|
The behavior when seconds have more than 6 digits or microseconds is too large |
'truncate'
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
|
datetime_schema ¶
datetime_schema(
*,
strict: bool | None = None,
le: datetime | None = None,
ge: datetime | None = None,
lt: datetime | None = None,
gt: datetime | None = None,
now_op: Literal["past", "future"] | None = None,
tz_constraint: Literal["aware", "naive"]
| int
| None = None,
now_utc_offset: int | None = None,
microseconds_precision: Literal[
"truncate", "error"
] = "truncate",
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> DatetimeSchema
Returns a schema that matches a datetime value, e.g.:
from datetime import datetime
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.datetime_schema()
v = SchemaValidator(schema)
now = datetime.now()
assert v.validate_python(str(now)) == now
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
Whether the value should be a datetime or a value that can be converted to a datetime |
None
|
le |
datetime | None
|
The value must be less than or equal to this datetime |
None
|
ge |
datetime | None
|
The value must be greater than or equal to this datetime |
None
|
lt |
datetime | None
|
The value must be strictly less than this datetime |
None
|
gt |
datetime | None
|
The value must be strictly greater than this datetime |
None
|
now_op |
Literal['past', 'future'] | None
|
The value must be in the past or future relative to the current datetime |
None
|
tz_constraint |
Literal['aware', 'naive'] | int | None
|
The value must be timezone aware or naive, or an int to indicate required tz offset TODO: use of a tzinfo where offset changes based on the datetime is not yet supported |
None
|
now_utc_offset |
int | None
|
The value must be in the past or future relative to the current datetime with this utc offset |
None
|
microseconds_precision |
Literal['truncate', 'error']
|
The behavior when seconds have more than 6 digits or microseconds is too large |
'truncate'
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
|
timedelta_schema ¶
timedelta_schema(
*,
strict: bool | None = None,
le: timedelta | None = None,
ge: timedelta | None = None,
lt: timedelta | None = None,
gt: timedelta | None = None,
microseconds_precision: Literal[
"truncate", "error"
] = "truncate",
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> TimedeltaSchema
Returns a schema that matches a timedelta value, e.g.:
from datetime import timedelta
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.timedelta_schema(le=timedelta(days=1), ge=timedelta(days=0))
v = SchemaValidator(schema)
assert v.validate_python(timedelta(hours=12)) == timedelta(hours=12)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
Whether the value should be a timedelta or a value that can be converted to a timedelta |
None
|
le |
timedelta | None
|
The value must be less than or equal to this timedelta |
None
|
ge |
timedelta | None
|
The value must be greater than or equal to this timedelta |
None
|
lt |
timedelta | None
|
The value must be strictly less than this timedelta |
None
|
gt |
timedelta | None
|
The value must be strictly greater than this timedelta |
None
|
microseconds_precision |
Literal['truncate', 'error']
|
The behavior when seconds have more than 6 digits or microseconds is too large |
'truncate'
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 |
|
literal_schema ¶
literal_schema(
expected: list[Any],
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> LiteralSchema
Returns a schema that matches a literal value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.literal_schema(['hello', 'world'])
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected |
list[Any]
|
The value must be one of these values |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 |
|
enum_schema ¶
enum_schema(
cls: Any,
members: list[Any],
*,
sub_type: Literal["str", "int", "float"] | None = None,
missing: Callable[[Any], Any] | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> EnumSchema
Returns a schema that matches an enum value, e.g.:
from enum import Enum
from pydantic_core import SchemaValidator, core_schema
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
schema = core_schema.enum_schema(Color, list(Color.__members__.values()))
v = SchemaValidator(schema)
assert v.validate_python(2) is Color.GREEN
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
The enum class |
required |
members |
list[Any]
|
The members of the enum, generally |
required |
sub_type |
Literal['str', 'int', 'float'] | None
|
The type of the enum, either 'str' or 'int' or None for plain enums |
None
|
missing |
Callable[[Any], Any] | None
|
A function to use when the value is not found in the enum, from |
None
|
strict |
bool | None
|
Whether to use strict mode, defaults to False |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 |
|
is_instance_schema ¶
is_instance_schema(
cls: Any,
*,
cls_repr: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> IsInstanceSchema
Returns a schema that checks if a value is an instance of a class, equivalent to python's isinstance
method, e.g.:
from pydantic_core import SchemaValidator, core_schema
class A:
pass
schema = core_schema.is_instance_schema(cls=A)
v = SchemaValidator(schema)
v.validate_python(A())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
The value must be an instance of this class |
required |
cls_repr |
str | None
|
If provided this string is used in the validator name instead of |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 |
|
is_subclass_schema ¶
is_subclass_schema(
cls: Type[Any],
*,
cls_repr: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> IsInstanceSchema
Returns a schema that checks if a value is a subtype of a class, equivalent to python's issubclass
method, e.g.:
from pydantic_core import SchemaValidator, core_schema
class A:
pass
class B(A):
pass
schema = core_schema.is_subclass_schema(cls=A)
v = SchemaValidator(schema)
v.validate_python(B)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type[Any]
|
The value must be a subclass of this class |
required |
cls_repr |
str | None
|
If provided this string is used in the validator name instead of |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 |
|
callable_schema ¶
callable_schema(
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> CallableSchema
Returns a schema that checks if a value is callable, equivalent to python's callable
method, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.callable_schema()
v = SchemaValidator(schema)
v.validate_python(min)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
|
list_schema ¶
list_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None
) -> ListSchema
Returns a schema that matches a list value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.list_schema(core_schema.int_schema(), min_length=0, max_length=10)
v = SchemaValidator(schema)
assert v.validate_python(['4']) == [4]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
CoreSchema | None
|
The value must be a list of items that match this schema |
None
|
min_length |
int | None
|
The value must be a list with at least this many items |
None
|
max_length |
int | None
|
The value must be a list with at most this many items |
None
|
strict |
bool | None
|
The value must be a list with exactly this many items |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
IncExSeqOrElseSerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 |
|
tuple_positional_schema ¶
tuple_positional_schema(
items_schema: list[CoreSchema],
*,
extras_schema: CoreSchema | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None
) -> TupleSchema
Returns a schema that matches a tuple of schemas, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.tuple_positional_schema(
[core_schema.int_schema(), core_schema.str_schema()]
)
v = SchemaValidator(schema)
assert v.validate_python((1, 'hello')) == (1, 'hello')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
list[CoreSchema]
|
The value must be a tuple with items that match these schemas |
required |
extras_schema |
CoreSchema | None
|
The value must be a tuple with items that match this schema
This was inspired by JSON schema's |
None
|
strict |
bool | None
|
The value must be a tuple with exactly this many items |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
IncExSeqOrElseSerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 1492 1493 1494 1495 1496 |
|
tuple_variable_schema ¶
tuple_variable_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None
) -> TupleSchema
Returns a schema that matches a tuple of a given schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.tuple_variable_schema(
items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python(('1', 2, 3)) == (1, 2, 3)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
CoreSchema | None
|
The value must be a tuple with items that match this schema |
None
|
min_length |
int | None
|
The value must be a tuple with at least this many items |
None
|
max_length |
int | None
|
The value must be a tuple with at most this many items |
None
|
strict |
bool | None
|
The value must be a tuple with exactly this many items |
None
|
ref |
str | None
|
Optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
IncExSeqOrElseSerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 |
|
tuple_schema ¶
tuple_schema(
items_schema: list[CoreSchema],
*,
variadic_item_index: int | None = None,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None
) -> TupleSchema
Returns a schema that matches a tuple of schemas, with an optional variadic item, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.tuple_schema(
[core_schema.int_schema(), core_schema.str_schema(), core_schema.float_schema()],
variadic_item_index=1,
)
v = SchemaValidator(schema)
assert v.validate_python((1, 'hello', 'world', 1.5)) == (1, 'hello', 'world', 1.5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
list[CoreSchema]
|
The value must be a tuple with items that match these schemas |
required |
variadic_item_index |
int | None
|
The index of the schema in |
None
|
min_length |
int | None
|
The value must be a tuple with at least this many items |
None
|
max_length |
int | None
|
The value must be a tuple with at most this many items |
None
|
strict |
bool | None
|
The value must be a tuple with exactly this many items |
None
|
ref |
str | None
|
Optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
IncExSeqOrElseSerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 |
|
set_schema ¶
set_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> SetSchema
Returns a schema that matches a set of a given schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.set_schema(
items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python({1, '2', 3}) == {1, 2, 3}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
CoreSchema | None
|
The value must be a set with items that match this schema |
None
|
min_length |
int | None
|
The value must be a set with at least this many items |
None
|
max_length |
int | None
|
The value must be a set with at most this many items |
None
|
strict |
bool | None
|
The value must be a set with exactly this many items |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 |
|
frozenset_schema ¶
frozenset_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> FrozenSetSchema
Returns a schema that matches a frozenset of a given schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.frozenset_schema(
items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python(frozenset(range(3))) == frozenset({0, 1, 2})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
CoreSchema | None
|
The value must be a frozenset with items that match this schema |
None
|
min_length |
int | None
|
The value must be a frozenset with at least this many items |
None
|
max_length |
int | None
|
The value must be a frozenset with at most this many items |
None
|
strict |
bool | None
|
The value must be a frozenset with exactly this many items |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 |
|
generator_schema ¶
generator_schema(
items_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None
) -> GeneratorSchema
Returns a schema that matches a generator value, e.g.:
from typing import Iterator
from pydantic_core import SchemaValidator, core_schema
def gen() -> Iterator[int]:
yield 1
schema = core_schema.generator_schema(items_schema=core_schema.int_schema())
v = SchemaValidator(schema)
v.validate_python(gen())
Unlike other types, validated generators do not raise ValidationErrors eagerly, but instead will raise a ValidationError when a violating value is actually read from the generator. This is to ensure that "validated" generators retain the benefit of lazy evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items_schema |
CoreSchema | None
|
The value must be a generator with items that match this schema |
None
|
min_length |
int | None
|
The value must be a generator that yields at least this many items |
None
|
max_length |
int | None
|
The value must be a generator that yields at most this many items |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
IncExSeqOrElseSerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 |
|
dict_schema ¶
dict_schema(
keys_schema: CoreSchema | None = None,
values_schema: CoreSchema | None = None,
*,
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> DictSchema
Returns a schema that matches a dict value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.dict_schema(
keys_schema=core_schema.str_schema(), values_schema=core_schema.int_schema()
)
v = SchemaValidator(schema)
assert v.validate_python({'a': '1', 'b': 2}) == {'a': 1, 'b': 2}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys_schema |
CoreSchema | None
|
The value must be a dict with keys that match this schema |
None
|
values_schema |
CoreSchema | None
|
The value must be a dict with values that match this schema |
None
|
min_length |
int | None
|
The value must be a dict with at least this many items |
None
|
max_length |
int | None
|
The value must be a dict with at most this many items |
None
|
strict |
bool | None
|
Whether the keys and values should be validated with strict mode |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 |
|
no_info_before_validator_function ¶
no_info_before_validator_function(
function: NoInfoValidatorFunction,
schema: CoreSchema,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> BeforeValidatorFunctionSchema
Returns a schema that calls a validator function before validating, no info
argument is provided, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: bytes) -> str:
return v.decode() + 'world'
func_schema = core_schema.no_info_before_validator_function(
function=fn, schema=core_schema.str_schema()
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
NoInfoValidatorFunction
|
The validator function to call |
required |
schema |
CoreSchema
|
The schema to validate the output of the validator function |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1880 1881 1882 1883 1884 1885 1886 1887 1888 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 |
|
with_info_before_validator_function ¶
with_info_before_validator_function(
function: WithInfoValidatorFunction,
schema: CoreSchema,
*,
field_name: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> BeforeValidatorFunctionSchema
Returns a schema that calls a validator function before validation, the function is called with
an info
argument, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
assert info.data is not None
assert info.field_name is not None
return v.decode() + 'world'
func_schema = core_schema.with_info_before_validator_function(
function=fn, schema=core_schema.str_schema(), field_name='a'
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
WithInfoValidatorFunction
|
The validator function to call |
required |
field_name |
str | None
|
The name of the field |
None
|
schema |
CoreSchema
|
The schema to validate the output of the validator function |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 |
|
no_info_after_validator_function ¶
no_info_after_validator_function(
function: NoInfoValidatorFunction,
schema: CoreSchema,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> AfterValidatorFunctionSchema
Returns a schema that calls a validator function after validating, no info
argument is provided, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str) -> str:
return v + 'world'
func_schema = core_schema.no_info_after_validator_function(fn, core_schema.str_schema())
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
NoInfoValidatorFunction
|
The validator function to call after the schema is validated |
required |
schema |
CoreSchema
|
The schema to validate before the validator function |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
1975 1976 1977 1978 1979 1980 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 |
|
with_info_after_validator_function ¶
with_info_after_validator_function(
function: WithInfoValidatorFunction,
schema: CoreSchema,
*,
field_name: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> AfterValidatorFunctionSchema
Returns a schema that calls a validator function after validation, the function is called with
an info
argument, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str, info: core_schema.ValidationInfo) -> str:
assert info.data is not None
assert info.field_name is not None
return v + 'world'
func_schema = core_schema.with_info_after_validator_function(
function=fn, schema=core_schema.str_schema(), field_name='a'
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
WithInfoValidatorFunction
|
The validator function to call after the schema is validated |
required |
schema |
CoreSchema
|
The schema to validate before the validator function |
required |
field_name |
str | None
|
The name of the field this validators is applied to, if any |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 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 |
|
no_info_wrap_validator_function ¶
no_info_wrap_validator_function(
function: NoInfoWrapValidatorFunction,
schema: CoreSchema,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> WrapValidatorFunctionSchema
Returns a schema which calls a function with a validator
callable argument which can
optionally be used to call inner validation with the function logic, this is much like the
"onion" implementation of middleware in many popular web frameworks, no info
argument is passed, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(
v: str,
validator: core_schema.ValidatorFunctionWrapHandler,
) -> str:
return validator(input_value=v) + 'world'
schema = core_schema.no_info_wrap_validator_function(
function=fn, schema=core_schema.str_schema()
)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
NoInfoWrapValidatorFunction
|
The validator function to call |
required |
schema |
CoreSchema
|
The schema to validate the output of the validator function |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 |
|
with_info_wrap_validator_function ¶
with_info_wrap_validator_function(
function: WithInfoWrapValidatorFunction,
schema: CoreSchema,
*,
field_name: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> WrapValidatorFunctionSchema
Returns a schema which calls a function with a validator
callable argument which can
optionally be used to call inner validation with the function logic, this is much like the
"onion" implementation of middleware in many popular web frameworks, an info
argument is also passed, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(
v: str,
validator: core_schema.ValidatorFunctionWrapHandler,
info: core_schema.ValidationInfo,
) -> str:
return validator(input_value=v) + 'world'
schema = core_schema.with_info_wrap_validator_function(
function=fn, schema=core_schema.str_schema()
)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
WithInfoWrapValidatorFunction
|
The validator function to call |
required |
schema |
CoreSchema
|
The schema to validate the output of the validator function |
required |
field_name |
str | None
|
The name of the field this validators is applied to, if any |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2146 2147 2148 2149 2150 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 2185 2186 2187 2188 2189 2190 2191 2192 |
|
no_info_plain_validator_function ¶
no_info_plain_validator_function(
function: NoInfoValidatorFunction,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> PlainValidatorFunctionSchema
Returns a schema that uses the provided function for validation, no info
argument is passed, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str) -> str:
assert 'hello' in v
return v + 'world'
schema = core_schema.no_info_plain_validator_function(function=fn)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
NoInfoValidatorFunction
|
The validator function to call |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 |
|
with_info_plain_validator_function ¶
with_info_plain_validator_function(
function: WithInfoValidatorFunction,
*,
field_name: str | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> PlainValidatorFunctionSchema
Returns a schema that uses the provided function for validation, an info
argument is passed, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str, info: core_schema.ValidationInfo) -> str:
assert 'hello' in v
return v + 'world'
schema = core_schema.with_info_plain_validator_function(function=fn)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
WithInfoValidatorFunction
|
The validator function to call |
required |
field_name |
str | None
|
The name of the field this validators is applied to, if any |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2240 2241 2242 2243 2244 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 2272 2273 2274 2275 2276 |
|
with_default_schema ¶
with_default_schema(
schema: CoreSchema,
*,
default: Any = PydanticUndefined,
default_factory: Callable[[], Any] | None = None,
on_error: Literal["raise", "omit", "default"]
| None = None,
validate_default: bool | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> WithDefaultSchema
Returns a schema that adds a default value to the given schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.with_default_schema(core_schema.str_schema(), default='hello')
wrapper_schema = core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(schema)}
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({}) == v.validate_python({'a': 'hello'})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The schema to add a default value to |
required |
default |
Any
|
The default value to use |
PydanticUndefined
|
default_factory |
Callable[[], Any] | None
|
A function that returns the default value to use |
None
|
on_error |
Literal['raise', 'omit', 'default'] | None
|
What to do if the schema validation fails. One of 'raise', 'omit', 'default' |
None
|
validate_default |
bool | None
|
Whether the default value should be validated |
None
|
strict |
bool | None
|
Whether the underlying schema should be validated with strict mode |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 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 2339 2340 2341 2342 |
|
nullable_schema ¶
nullable_schema(
schema: CoreSchema,
*,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> NullableSchema
Returns a schema that matches a nullable value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.nullable_schema(core_schema.str_schema())
v = SchemaValidator(schema)
assert v.validate_python(None) is None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The schema to wrap |
required |
strict |
bool | None
|
Whether the underlying schema should be validated with strict mode |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 |
|
union_schema ¶
union_schema(
choices: list[CoreSchema | tuple[CoreSchema, str]],
*,
auto_collapse: bool | None = None,
custom_error_type: str | None = None,
custom_error_message: str | None = None,
custom_error_context: dict[str, str | int]
| None = None,
mode: Literal["smart", "left_to_right"] | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> UnionSchema
Returns a schema that matches a union value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.union_schema([core_schema.str_schema(), core_schema.int_schema()])
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
assert v.validate_python(1) == 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
choices |
list[CoreSchema | tuple[CoreSchema, str]]
|
The schemas to match. If a tuple, the second item is used as the label for the case. |
required |
auto_collapse |
bool | None
|
whether to automatically collapse unions with one element to the inner validator, default true |
None
|
custom_error_type |
str | None
|
The custom error type to use if the validation fails |
None
|
custom_error_message |
str | None
|
The custom error message to use if the validation fails |
None
|
custom_error_context |
dict[str, str | int] | None
|
The custom error context to use if the validation fails |
None
|
mode |
Literal['smart', 'left_to_right'] | None
|
How to select which choice to return
* |
None
|
strict |
bool | None
|
Whether the underlying schemas should be validated with strict mode |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 |
|
tagged_union_schema ¶
tagged_union_schema(
choices: Dict[Hashable, CoreSchema],
discriminator: str
| list[str | int]
| list[list[str | int]]
| Callable[[Any], Hashable],
*,
custom_error_type: str | None = None,
custom_error_message: str | None = None,
custom_error_context: dict[str, int | str | float]
| None = None,
strict: bool | None = None,
from_attributes: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> TaggedUnionSchema
Returns a schema that matches a tagged union value, e.g.:
from pydantic_core import SchemaValidator, core_schema
apple_schema = core_schema.typed_dict_schema(
{
'foo': core_schema.typed_dict_field(core_schema.str_schema()),
'bar': core_schema.typed_dict_field(core_schema.int_schema()),
}
)
banana_schema = core_schema.typed_dict_schema(
{
'foo': core_schema.typed_dict_field(core_schema.str_schema()),
'spam': core_schema.typed_dict_field(
core_schema.list_schema(items_schema=core_schema.int_schema())
),
}
)
schema = core_schema.tagged_union_schema(
choices={
'apple': apple_schema,
'banana': banana_schema,
},
discriminator='foo',
)
v = SchemaValidator(schema)
assert v.validate_python({'foo': 'apple', 'bar': '123'}) == {'foo': 'apple', 'bar': 123}
assert v.validate_python({'foo': 'banana', 'spam': [1, 2, 3]}) == {
'foo': 'banana',
'spam': [1, 2, 3],
}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
choices |
Dict[Hashable, CoreSchema]
|
The schemas to match
When retrieving a schema from |
required |
discriminator |
str | list[str | int] | list[list[str | int]] | Callable[[Any], Hashable]
|
The discriminator to use to determine the schema to use
* If |
required |
custom_error_type |
str | None
|
The custom error type to use if the validation fails |
None
|
custom_error_message |
str | None
|
The custom error message to use if the validation fails |
None
|
custom_error_context |
dict[str, int | str | float] | None
|
The custom error context to use if the validation fails |
None
|
strict |
bool | None
|
Whether the underlying schemas should be validated with strict mode |
None
|
from_attributes |
bool | None
|
Whether to use the attributes of the object to retrieve the discriminator value |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 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 2543 2544 2545 2546 2547 2548 |
|
chain_schema ¶
chain_schema(
steps: list[CoreSchema],
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> ChainSchema
Returns a schema that chains the provided validation schemas, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str, info: core_schema.ValidationInfo) -> str:
assert 'hello' in v
return v + ' world'
fn_schema = core_schema.with_info_plain_validator_function(function=fn)
schema = core_schema.chain_schema(
[fn_schema, fn_schema, fn_schema, core_schema.str_schema()]
)
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello world world world'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
steps |
list[CoreSchema]
|
The schemas to chain |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
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 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(
lax_schema: CoreSchema,
strict_schema: CoreSchema,
*,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> LaxOrStrictSchema
Returns a schema that uses the lax or strict schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
def fn(v: str, info: core_schema.ValidationInfo) -> str:
assert 'hello' in v
return v + ' world'
lax_schema = core_schema.int_schema(strict=False)
strict_schema = core_schema.int_schema(strict=True)
schema = core_schema.lax_or_strict_schema(
lax_schema=lax_schema, strict_schema=strict_schema, strict=True
)
v = SchemaValidator(schema)
assert v.validate_python(123) == 123
schema = core_schema.lax_or_strict_schema(
lax_schema=lax_schema, strict_schema=strict_schema, strict=False
)
v = SchemaValidator(schema)
assert v.validate_python('123') == 123
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lax_schema |
CoreSchema
|
The lax schema to use |
required |
strict_schema |
CoreSchema
|
The strict schema to use |
required |
strict |
bool | None
|
Whether the strict schema should be used |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 |
|
json_or_python_schema ¶
json_or_python_schema(
json_schema: CoreSchema,
python_schema: CoreSchema,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> JsonOrPythonSchema
Returns a schema that uses the Json or Python schema depending on the input:
from pydantic_core import SchemaValidator, ValidationError, core_schema
v = SchemaValidator(
core_schema.json_or_python_schema(
json_schema=core_schema.int_schema(),
python_schema=core_schema.int_schema(strict=True),
)
)
assert v.validate_json('"123"') == 123
try:
v.validate_python('123')
except ValidationError:
pass
else:
raise AssertionError('Validation should have failed')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
CoreSchema
|
The schema to use for Json inputs |
required |
python_schema |
CoreSchema
|
The schema to use for Python inputs |
required |
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 |
|
typed_dict_field ¶
typed_dict_field(
schema: CoreSchema,
*,
required: bool | None = None,
validation_alias: str
| list[str | int]
| list[list[str | int]]
| None = None,
serialization_alias: str | None = None,
serialization_exclude: bool | None = None,
metadata: Any = None
) -> TypedDictField
Returns a schema that matches a typed dict field, e.g.:
from pydantic_core import core_schema
field = core_schema.typed_dict_field(schema=core_schema.int_schema(), required=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The schema to use for the field |
required |
required |
bool | None
|
Whether the field is required |
None
|
validation_alias |
str | list[str | int] | list[list[str | int]] | None
|
The alias(es) to use to find the field in the validation data |
None
|
serialization_alias |
str | None
|
The alias to use as a key when serializing |
None
|
serialization_exclude |
bool | None
|
Whether to exclude the field when serializing |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
Source code in pydantic_core/core_schema.py
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 |
|
typed_dict_schema ¶
typed_dict_schema(
fields: Dict[str, TypedDictField],
*,
computed_fields: list[ComputedField] | None = None,
strict: bool | None = None,
extras_schema: CoreSchema | None = None,
extra_behavior: ExtraBehavior | None = None,
total: bool | None = None,
populate_by_name: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None,
config: CoreConfig | None = None
) -> TypedDictSchema
Returns a schema that matches a typed dict, e.g.:
from pydantic_core import SchemaValidator, core_schema
wrapper_schema = core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(core_schema.str_schema())}
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
Dict[str, TypedDictField]
|
The fields to use for the typed dict |
required |
computed_fields |
list[ComputedField] | None
|
Computed fields to use when serializing the model, only applies when directly inside a model |
None
|
strict |
bool | None
|
Whether the typed dict is strict |
None
|
extras_schema |
CoreSchema | None
|
The extra validator to use for the typed dict |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
extra_behavior |
ExtraBehavior | None
|
The extra behavior to use for the typed dict |
None
|
total |
bool | None
|
Whether the typed dict is total |
None
|
populate_by_name |
bool | None
|
Whether the typed dict should populate by name |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 |
|
model_field ¶
model_field(
schema: CoreSchema,
*,
validation_alias: str
| list[str | int]
| list[list[str | int]]
| None = None,
serialization_alias: str | None = None,
serialization_exclude: bool | None = None,
frozen: bool | None = None,
metadata: Any = None
) -> ModelField
Returns a schema for a model field, e.g.:
from pydantic_core import core_schema
field = core_schema.model_field(schema=core_schema.int_schema())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The schema to use for the field |
required |
validation_alias |
str | list[str | int] | list[list[str | int]] | None
|
The alias(es) to use to find the field in the validation data |
None
|
serialization_alias |
str | None
|
The alias to use as a key when serializing |
None
|
serialization_exclude |
bool | None
|
Whether to exclude the field when serializing |
None
|
frozen |
bool | None
|
Whether the field is frozen |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
Source code in pydantic_core/core_schema.py
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 |
|
model_fields_schema ¶
model_fields_schema(
fields: Dict[str, ModelField],
*,
model_name: str | None = None,
computed_fields: list[ComputedField] | None = None,
strict: bool | None = None,
extras_schema: CoreSchema | None = None,
extra_behavior: ExtraBehavior | None = None,
populate_by_name: bool | None = None,
from_attributes: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> ModelFieldsSchema
Returns a schema that matches a typed dict, e.g.:
from pydantic_core import SchemaValidator, core_schema
wrapper_schema = core_schema.model_fields_schema(
{'a': core_schema.model_field(core_schema.str_schema())}
)
v = SchemaValidator(wrapper_schema)
print(v.validate_python({'a': 'hello'}))
#> ({'a': 'hello'}, None, {'a'})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
Dict[str, ModelField]
|
The fields to use for the typed dict |
required |
model_name |
str | None
|
The name of the model, used for error messages, defaults to "Model" |
None
|
computed_fields |
list[ComputedField] | None
|
Computed fields to use when serializing the model, only applies when directly inside a model |
None
|
strict |
bool | None
|
Whether the typed dict is strict |
None
|
extras_schema |
CoreSchema | None
|
The extra validator to use for the typed dict |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
extra_behavior |
ExtraBehavior | None
|
The extra behavior to use for the typed dict |
None
|
populate_by_name |
bool | None
|
Whether the typed dict should populate by name |
None
|
from_attributes |
bool | None
|
Whether the typed dict should be populated from attributes |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 |
|
model_schema ¶
model_schema(
cls: Type[Any],
schema: CoreSchema,
*,
custom_init: bool | None = None,
root_model: bool | None = None,
post_init: str | None = None,
revalidate_instances: Literal[
"always", "never", "subclass-instances"
]
| None = None,
strict: bool | None = None,
frozen: bool | None = None,
extra_behavior: ExtraBehavior | None = None,
config: CoreConfig | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> ModelSchema
A model schema generally contains a typed-dict schema.
It will run the typed dict validator, then create a new class
and set the dict and fields set returned from the typed dict validator
to __dict__
and __pydantic_fields_set__
respectively.
Example:
from pydantic_core import CoreConfig, SchemaValidator, core_schema
class MyModel:
__slots__ = (
'__dict__',
'__pydantic_fields_set__',
'__pydantic_extra__',
'__pydantic_private__',
)
schema = core_schema.model_schema(
cls=MyModel,
config=CoreConfig(str_max_length=5),
schema=core_schema.model_fields_schema(
fields={'a': core_schema.model_field(core_schema.str_schema())},
),
)
v = SchemaValidator(schema)
assert v.isinstance_python({'a': 'hello'}) is True
assert v.isinstance_python({'a': 'too long'}) is False
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type[Any]
|
The class to use for the model |
required |
schema |
CoreSchema
|
The schema to use for the model |
required |
custom_init |
bool | None
|
Whether the model has a custom init method |
None
|
root_model |
bool | None
|
Whether the model is a |
None
|
post_init |
str | None
|
The call after init to use for the model |
None
|
revalidate_instances |
Literal['always', 'never', 'subclass-instances'] | None
|
whether instances of models and dataclasses (including subclass instances) should re-validate defaults to config.revalidate_instances, else 'never' |
None
|
strict |
bool | None
|
Whether the model is strict |
None
|
frozen |
bool | None
|
Whether the model is frozen |
None
|
extra_behavior |
ExtraBehavior | None
|
The extra behavior to use for the model, used in serialization |
None
|
config |
CoreConfig | None
|
The config to use for the model |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 |
|
dataclass_field ¶
dataclass_field(
name: str,
schema: CoreSchema,
*,
kw_only: bool | None = None,
init: bool | None = None,
init_only: bool | None = None,
validation_alias: str
| list[str | int]
| list[list[str | int]]
| None = None,
serialization_alias: str | None = None,
serialization_exclude: bool | None = None,
metadata: Any = None,
frozen: bool | None = None
) -> DataclassField
Returns a schema for a dataclass field, e.g.:
from pydantic_core import SchemaValidator, core_schema
field = core_schema.dataclass_field(
name='a', schema=core_schema.str_schema(), kw_only=False
)
schema = core_schema.dataclass_args_schema('Foobar', [field])
v = SchemaValidator(schema)
assert v.validate_python({'a': 'hello'}) == ({'a': 'hello'}, None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to use for the argument parameter |
required |
schema |
CoreSchema
|
The schema to use for the argument parameter |
required |
kw_only |
bool | None
|
Whether the field can be set with a positional argument as well as a keyword argument |
None
|
init |
bool | None
|
Whether the field should be validated during initialization |
None
|
init_only |
bool | None
|
Whether the field should be omitted from |
None
|
validation_alias |
str | list[str | int] | list[list[str | int]] | None
|
The alias(es) to use to find the field in the validation data |
None
|
serialization_alias |
str | None
|
The alias to use as a key when serializing |
None
|
serialization_exclude |
bool | None
|
Whether to exclude the field when serializing |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
frozen |
bool | None
|
Whether the field is frozen |
None
|
Source code in pydantic_core/core_schema.py
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 |
|
dataclass_args_schema ¶
dataclass_args_schema(
dataclass_name: str,
fields: list[DataclassField],
*,
computed_fields: List[ComputedField] | None = None,
populate_by_name: bool | None = None,
collect_init_only: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None,
extra_behavior: ExtraBehavior | None = None
) -> DataclassArgsSchema
Returns a schema for validating dataclass arguments, e.g.:
from pydantic_core import SchemaValidator, core_schema
field_a = core_schema.dataclass_field(
name='a', schema=core_schema.str_schema(), kw_only=False
)
field_b = core_schema.dataclass_field(
name='b', schema=core_schema.bool_schema(), kw_only=False
)
schema = core_schema.dataclass_args_schema('Foobar', [field_a, field_b])
v = SchemaValidator(schema)
assert v.validate_python({'a': 'hello', 'b': True}) == ({'a': 'hello', 'b': True}, None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataclass_name |
str
|
The name of the dataclass being validated |
required |
fields |
list[DataclassField]
|
The fields to use for the dataclass |
required |
computed_fields |
List[ComputedField] | None
|
Computed fields to use when serializing the dataclass |
None
|
populate_by_name |
bool | None
|
Whether to populate by name |
None
|
collect_init_only |
bool | None
|
Whether to collect init only fields into a dict to pass to |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
extra_behavior |
ExtraBehavior | None
|
How to handle extra fields |
None
|
Source code in pydantic_core/core_schema.py
3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 |
|
dataclass_schema ¶
dataclass_schema(
cls: Type[Any],
schema: CoreSchema,
fields: List[str],
*,
cls_name: str | None = None,
post_init: bool | None = None,
revalidate_instances: Literal[
"always", "never", "subclass-instances"
]
| None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None,
frozen: bool | None = None,
slots: bool | None = None,
config: CoreConfig | None = None
) -> DataclassSchema
Returns a schema for a dataclass. As with ModelSchema
, this schema can only be used as a field within
another schema, not as the root type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type[Any]
|
The dataclass type, used to perform subclass checks |
required |
schema |
CoreSchema
|
The schema to use for the dataclass fields |
required |
fields |
List[str]
|
Fields of the dataclass, this is used in serialization and in validation during re-validation and while validating assignment |
required |
cls_name |
str | None
|
The name to use in error locs, etc; this is useful for generics (default: |
None
|
post_init |
bool | None
|
Whether to call |
None
|
revalidate_instances |
Literal['always', 'never', 'subclass-instances'] | None
|
whether instances of models and dataclasses (including subclass instances) should re-validate defaults to config.revalidate_instances, else 'never' |
None
|
strict |
bool | None
|
Whether to require an exact instance of |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
frozen |
bool | None
|
Whether the dataclass is frozen |
None
|
slots |
bool | None
|
Whether |
None
|
Source code in pydantic_core/core_schema.py
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 |
|
arguments_parameter ¶
arguments_parameter(
name: str,
schema: CoreSchema,
*,
mode: Literal[
"positional_only",
"positional_or_keyword",
"keyword_only",
]
| None = None,
alias: str
| list[str | int]
| list[list[str | int]]
| None = None
) -> ArgumentsParameter
Returns a schema that matches an argument parameter, e.g.:
from pydantic_core import SchemaValidator, core_schema
param = core_schema.arguments_parameter(
name='a', schema=core_schema.str_schema(), mode='positional_only'
)
schema = core_schema.arguments_schema([param])
v = SchemaValidator(schema)
assert v.validate_python(('hello',)) == (('hello',), {})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to use for the argument parameter |
required |
schema |
CoreSchema
|
The schema to use for the argument parameter |
required |
mode |
Literal['positional_only', 'positional_or_keyword', 'keyword_only'] | None
|
The mode to use for the argument parameter |
None
|
alias |
str | list[str | int] | list[list[str | int]] | None
|
The alias to use for the argument parameter |
None
|
Source code in pydantic_core/core_schema.py
3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 |
|
arguments_schema ¶
arguments_schema(
arguments: list[ArgumentsParameter],
*,
populate_by_name: bool | None = None,
var_args_schema: CoreSchema | None = None,
var_kwargs_schema: CoreSchema | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> ArgumentsSchema
Returns a schema that matches an arguments schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
param_a = core_schema.arguments_parameter(
name='a', schema=core_schema.str_schema(), mode='positional_only'
)
param_b = core_schema.arguments_parameter(
name='b', schema=core_schema.bool_schema(), mode='positional_only'
)
schema = core_schema.arguments_schema([param_a, param_b])
v = SchemaValidator(schema)
assert v.validate_python(('hello', True)) == (('hello', True), {})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[ArgumentsParameter]
|
The arguments to use for the arguments schema |
required |
populate_by_name |
bool | None
|
Whether to populate by name |
None
|
var_args_schema |
CoreSchema | None
|
The variable args schema to use for the arguments schema |
None
|
var_kwargs_schema |
CoreSchema | None
|
The variable kwargs schema to use for the arguments schema |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 |
|
call_schema ¶
call_schema(
arguments: CoreSchema,
function: Callable[..., Any],
*,
function_name: str | None = None,
return_schema: CoreSchema | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> CallSchema
Returns a schema that matches an arguments schema, then calls a function, e.g.:
from pydantic_core import SchemaValidator, core_schema
param_a = core_schema.arguments_parameter(
name='a', schema=core_schema.str_schema(), mode='positional_only'
)
param_b = core_schema.arguments_parameter(
name='b', schema=core_schema.bool_schema(), mode='positional_only'
)
args_schema = core_schema.arguments_schema([param_a, param_b])
schema = core_schema.call_schema(
arguments=args_schema,
function=lambda a, b: a + str(not b),
return_schema=core_schema.str_schema(),
)
v = SchemaValidator(schema)
assert v.validate_python((('hello', True))) == 'helloFalse'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
CoreSchema
|
The arguments to use for the arguments schema |
required |
function |
Callable[..., Any]
|
The function to use for the call schema |
required |
function_name |
str | None
|
The function name to use for the call schema, if not provided |
None
|
return_schema |
CoreSchema | None
|
The return schema to use for the call schema |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 |
|
custom_error_schema ¶
custom_error_schema(
schema: CoreSchema,
custom_error_type: str,
*,
custom_error_message: str | None = None,
custom_error_context: dict[str, Any] | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> CustomErrorSchema
Returns a schema that matches a custom error value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.custom_error_schema(
schema=core_schema.int_schema(),
custom_error_type='MyError',
custom_error_message='Error msg',
)
v = SchemaValidator(schema)
v.validate_python(1)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The schema to use for the custom error schema |
required |
custom_error_type |
str
|
The custom error type to use for the custom error schema |
required |
custom_error_message |
str | None
|
The custom error message to use for the custom error schema |
None
|
custom_error_context |
dict[str, Any] | None
|
The custom error context to use for the custom error schema |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 |
|
json_schema ¶
json_schema(
schema: CoreSchema | None = None,
*,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> JsonSchema
Returns a schema that matches a JSON value, e.g.:
from pydantic_core import SchemaValidator, core_schema
dict_schema = core_schema.model_fields_schema(
{
'field_a': core_schema.model_field(core_schema.str_schema()),
'field_b': core_schema.model_field(core_schema.bool_schema()),
},
)
class MyModel:
__slots__ = (
'__dict__',
'__pydantic_fields_set__',
'__pydantic_extra__',
'__pydantic_private__',
)
field_a: str
field_b: bool
json_schema = core_schema.json_schema(schema=dict_schema)
schema = core_schema.model_schema(cls=MyModel, schema=json_schema)
v = SchemaValidator(schema)
m = v.validate_python('{"field_a": "hello", "field_b": true}')
assert isinstance(m, MyModel)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema | None
|
The schema to use for the JSON schema |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 |
|
url_schema ¶
url_schema(
*,
max_length: int | None = None,
allowed_schemes: list[str] | None = None,
host_required: bool | None = None,
default_host: str | None = None,
default_port: int | None = None,
default_path: str | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> UrlSchema
Returns a schema that matches a URL value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.url_schema()
v = SchemaValidator(schema)
print(v.validate_python('https://example.com'))
#> https://example.com/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_length |
int | None
|
The maximum length of the URL |
None
|
allowed_schemes |
list[str] | None
|
The allowed URL schemes |
None
|
host_required |
bool | None
|
Whether the URL must have a host |
None
|
default_host |
str | None
|
The default host to use if the URL does not have a host |
None
|
default_port |
int | None
|
The default port to use if the URL does not have a port |
None
|
default_path |
str | None
|
The default path to use if the URL does not have a path |
None
|
strict |
bool | None
|
Whether to use strict URL parsing |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 |
|
multi_host_url_schema ¶
multi_host_url_schema(
*,
max_length: int | None = None,
allowed_schemes: list[str] | None = None,
host_required: bool | None = None,
default_host: str | None = None,
default_port: int | None = None,
default_path: str | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None
) -> MultiHostUrlSchema
Returns a schema that matches a URL value with possibly multiple hosts, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.multi_host_url_schema()
v = SchemaValidator(schema)
print(v.validate_python('redis://localhost,0.0.0.0,127.0.0.1'))
#> redis://localhost,0.0.0.0,127.0.0.1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_length |
int | None
|
The maximum length of the URL |
None
|
allowed_schemes |
list[str] | None
|
The allowed URL schemes |
None
|
host_required |
bool | None
|
Whether the URL must have a host |
None
|
default_host |
str | None
|
The default host to use if the URL does not have a host |
None
|
default_port |
int | None
|
The default port to use if the URL does not have a port |
None
|
default_path |
str | None
|
The default path to use if the URL does not have a path |
None
|
strict |
bool | None
|
Whether to use strict URL parsing |
None
|
ref |
str | None
|
optional unique identifier of the schema, used to reference the schema in other places |
None
|
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 |
|
definitions_schema ¶
definitions_schema(
schema: CoreSchema, definitions: list[CoreSchema]
) -> DefinitionsSchema
Build a schema that contains both an inner schema and a list of definitions which can be used within the inner schema.
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.definitions_schema(
core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
[core_schema.int_schema(ref='foobar')],
)
v = SchemaValidator(schema)
assert v.validate_python([1, 2, '3']) == [1, 2, 3]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
The inner schema |
required |
definitions |
list[CoreSchema]
|
List of definitions which can be referenced within inner schema |
required |
Source code in pydantic_core/core_schema.py
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 |
|
definition_reference_schema ¶
definition_reference_schema(
schema_ref: str,
ref: str | None = None,
metadata: Any = None,
serialization: SerSchema | None = None,
) -> DefinitionReferenceSchema
Returns a schema that points to a schema stored in "definitions", this is useful for nested recursive models and also when you want to define validators separately from the main schema, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema_definition = core_schema.definition_reference_schema('list-schema')
schema = core_schema.definitions_schema(
schema=schema_definition,
definitions=[
core_schema.list_schema(items_schema=schema_definition, ref='list-schema'),
],
)
v = SchemaValidator(schema)
assert v.validate_python([()]) == [[]]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_ref |
str
|
The schema ref to use for the definition reference schema |
required |
metadata |
Any
|
Any other information you want to include with the schema, not used by pydantic-core |
None
|
serialization |
SerSchema | None
|
Custom serialization schema |
None
|
Source code in pydantic_core/core_schema.py
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 |
|