Skip to content

Commit e62e3cf

Browse files
stinovlashramezani
andauthored
Fix #166 - Add docs for validating default values (#168)
Co-authored-by: Hasan Ramezani <[email protected]>
1 parent 3205f81 commit e62e3cf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,40 @@ print(Settings().model_dump())
104104

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

107+
## Validation of default values
108+
109+
Unlike pydantic `BaseModel`, default values of `BaseSettings` fields are validated by default.
110+
You can disable this behaviour by setting `validate_default=False` either in `model_config`
111+
or on field level by `Field(validate_default=False)`:
112+
113+
```py
114+
from pydantic import Field
115+
116+
from pydantic_settings import BaseSettings, SettingsConfigDict
117+
118+
119+
class Settings(BaseSettings):
120+
model_config = SettingsConfigDict(validate_default=False)
121+
122+
# default won't be validated
123+
foo: int = 'test'
124+
125+
126+
print(Settings())
127+
#> foo='test'
128+
129+
130+
class Settings1(BaseSettings):
131+
# default won't be validated
132+
foo: int = Field('test', validate_default=False)
133+
134+
135+
print(Settings1())
136+
#> foo='test'
137+
```
138+
139+
Check the [Validation of default values](validators.md#validation-of-default-values) for more information.
140+
107141
## Environment variable names
108142

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

0 commit comments

Comments
 (0)