Skip to content

Commit

Permalink
Remove the Validate always section in validator docs (#6679)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jul 17, 2023
1 parent d9bf7f7 commit d249af9
Showing 1 changed file with 0 additions and 31 deletions.
31 changes: 0 additions & 31 deletions docs/usage/validators.md
Expand Up @@ -321,37 +321,6 @@ print(Model(a='invalid').a)
#> 2000-01-01 00:00:00
```

## Validate always

**TODO this content is wrong!**

For performance reasons, by default validators are not called for fields when a value is not supplied.
However there are situations where it may be useful or required to always call the validator, e.g.
to set a dynamic default value.

```py
from datetime import datetime

from pydantic import BaseModel, field_validator


class DemoModel(BaseModel):
ts: datetime

@field_validator('ts', mode='before')
def set_ts_now(cls, v):
return v or datetime.now()


print(DemoModel(ts=None))
#> ts=datetime.datetime(2032, 1, 2, 3, 4, 5, 6)
print(DemoModel(ts='2017-11-08T14:00'))
#> ts=datetime.datetime(2017, 11, 8, 14, 0)
```

You'll often want to use this together with `pre`, since otherwise with `always=True`
Pydantic would try to validate the default `None` which would cause an error.

## Reuse validators

Occasionally, you will want to use the same validator on multiple fields/models (e.g. to
Expand Down

0 comments on commit d249af9

Please sign in to comment.