Skip to content

Commit

Permalink
Clarify validate_default and _Unset handling in usage docs and migrat…
Browse files Browse the repository at this point in the history
…ion guide (#6950)
  • Loading branch information
benbenbang committed Aug 14, 2023
1 parent 0e6cb3e commit 35144d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/migration.md
Expand Up @@ -238,6 +238,10 @@ See [Model Config](usage/model_config.md) for more details.
for the annotated type will _also_ be applied even to defaults, not just the custom validators. For
example, despite the fact that the validator below will never error, the following code raises a `ValidationError`:

!!! note
To avoid this, you can use the `validate_default` argument in the `Field` function. When set to `True`, it mimics the behavior of `always=True` in Pydantic v1. However, the new way of using `validate_default` is encouraged as it provides more flexibility and control.


```python test="skip"
from pydantic import BaseModel, validator

Expand Down
3 changes: 2 additions & 1 deletion docs/usage/validators.md
Expand Up @@ -266,7 +266,8 @@ print(context['logs'])

Validators won't run when the default value is used.
This applies both to `@field_validator` validators and `Annotated` validators.
You can force them to run with `Field(validate_defaults=True)`, but you are generally better off using a `@model_validator(mode='before')`.
You can force them to run with `Field(validate_defaults=True)`. Setting `validate_default` to `True` has the closest behavior to using `always=True` in `validator` in Pydantic v1. However, you are generally better off using a `@model_validator(mode='before')` where the function is called before the inner validator is called.


```py
from typing_extensions import Annotated
Expand Down
5 changes: 4 additions & 1 deletion pydantic/fields.py
Expand Up @@ -701,6 +701,9 @@ def Field( # noqa: C901
Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
apply only to number fields (`int`, `float`, `Decimal`) and some apply only to `str`.
Note:
- Any `_Unset` objects will be replaced by the corresponding value defined in the `_DefaultValues` dictionary. If a key for the `_Unset` object is not found in the `_DefaultValues` dictionary, it will default to `None`
Args:
default: Default value if the field is not set.
default_factory: A callable to generate the default value, such as :func:`~datetime.utcnow`.
Expand All @@ -717,7 +720,7 @@ def Field( # noqa: C901
discriminator: Field name for discriminating the type in a tagged union.
json_schema_extra: Any additional JSON schema data for the schema property.
frozen: Whether the field is frozen.
validate_default: Run validation that isn't only checking existence of defaults. `True` by default.
validate_default: Run validation that isn't only checking existence of defaults. This can be set to `True` or `False`. If not set, it defaults to `None`.
repr: A boolean indicating whether to include the field in the `__repr__` output.
init_var: Whether the field should be included in the constructor of the dataclass.
kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
Expand Down

0 comments on commit 35144d0

Please sign in to comment.