Skip to content

Commit

Permalink
Fix #166 - Add docs for validating default values (#168)
Browse files Browse the repository at this point in the history
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
  • Loading branch information
stinovlas and hramezani authored Sep 18, 2023
1 parent 3205f81 commit e62e3cf
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 level by `Field(validate_default=False)`:

```py
from pydantic import Field

from pydantic_settings import BaseSettings, SettingsConfigDict


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

# default won't be validated
foo: int = 'test'


print(Settings())
#> foo='test'


class Settings1(BaseSettings):
# default won't be validated
foo: int = Field('test', validate_default=False)


print(Settings1())
#> foo='test'
```

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 e62e3cf

Please sign in to comment.