Skip to content

Commit

Permalink
Fix pydantic#166 - Add docs for validating default values
Browse files Browse the repository at this point in the history
  • Loading branch information
stinovlas committed Sep 15, 2023
1 parent 3205f81 commit 17ae470
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ print(Settings().model_dump())

Check the [Environment variable names documentation](#environment-variable-names) for more information.

## Validation of default values

Unlike pydantic `BaseModel`, default values of `BaseSettings` fields are validated by default.
You can disable this behaviour by setting `validate_default=False` either in `model_config`
or on field-by-field basis:

```py
import datetime as dt

from pydantic import Field
from pydantic.functional_validators import BeforeValidator
from typing_extensions import Annotated

from pydantic_settings import BaseSettings, SettingsConfigDict


class GlobalSettings(BaseSettings):
model_config = SettingsConfigDict(validate_default=False)

# default won't be validated
date: Annotated[dt.date, BeforeValidator(dt.date.fromisoformat)] = dt.date(
2000, 1, 1
)


class LocalSettings(BaseSettings):
# default won't be validated
date: Annotated[dt.date, BeforeValidator(dt.date.fromisoformat)] = Field(
default=dt.date(2000, 1, 1), validate_default=False
)
```

Check the [Validation of default values](validators.md#validation-of-default-values) for more information.

## Environment variable names

By default, the environment variable name is the same as the field name.
Expand Down

0 comments on commit 17ae470

Please sign in to comment.