Skip to content

Commit 76ba2c6

Browse files
authored
Fix context not passed to field validators bug (#417)
1 parent 84cab2b commit 76ba2c6

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pydantic_settings/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def __init__(
193193
)
194194
)
195195

196+
__init__.__pydantic_base_init__ = True # type: ignore
197+
196198
@classmethod
197199
def settings_customise_sources(
198200
cls,

tests/test_settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
SecretStr,
3131
Tag,
3232
ValidationError,
33+
ValidationInfo,
34+
field_validator,
3335
)
3436
from pydantic import (
3537
dataclasses as pydantic_dataclasses,
@@ -5187,6 +5189,22 @@ class Settings(BaseSettings):
51875189
assert s.model_dump() == {'nested': {'foo': ['one', 'two']}}
51885190

51895191

5192+
def test_validation_context():
5193+
class Settings(BaseSettings):
5194+
foo: str
5195+
5196+
@field_validator('foo')
5197+
@classmethod
5198+
def test_validator(cls, v: str, info: ValidationInfo):
5199+
context = info.context
5200+
assert context == {'foo': 'bar'}
5201+
return v
5202+
5203+
s = Settings.model_validate({'foo': 'foo bar'}, context={'foo': 'bar'})
5204+
assert s.foo == 'foo bar'
5205+
assert s.model_dump() == {'foo': 'foo bar'}
5206+
5207+
51905208
def test_nested_model_field_with_alias_choices(env):
51915209
class NestedSettings(BaseModel):
51925210
foo: List[str] = Field(alias=AliasChoices('fooalias', 'foo-alias'))

0 commit comments

Comments
 (0)