Skip to content

Commit

Permalink
Fix #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 6c60b87
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ 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


def str_to_date(v: str) -> dt.date:
return dt.date.fromisoformat(v)


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

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


class LocalSettings(BaseSettings):
# default won't be validated
date: Annotated[dt.date, BeforeValidator(str_to_date)] = 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 6c60b87

Please sign in to comment.