Skip to content

Commit

Permalink
Fix context not passed to field validators bug (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani authored Sep 24, 2024
1 parent 84cab2b commit 76ba2c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pydantic_settings/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def __init__(
)
)

__init__.__pydantic_base_init__ = True # type: ignore

@classmethod
def settings_customise_sources(
cls,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
SecretStr,
Tag,
ValidationError,
ValidationInfo,
field_validator,
)
from pydantic import (
dataclasses as pydantic_dataclasses,
Expand Down Expand Up @@ -5187,6 +5189,22 @@ class Settings(BaseSettings):
assert s.model_dump() == {'nested': {'foo': ['one', 'two']}}


def test_validation_context():
class Settings(BaseSettings):
foo: str

@field_validator('foo')
@classmethod
def test_validator(cls, v: str, info: ValidationInfo):
context = info.context
assert context == {'foo': 'bar'}
return v

s = Settings.model_validate({'foo': 'foo bar'}, context={'foo': 'bar'})
assert s.foo == 'foo bar'
assert s.model_dump() == {'foo': 'foo bar'}


def test_nested_model_field_with_alias_choices(env):
class NestedSettings(BaseModel):
foo: List[str] = Field(alias=AliasChoices('fooalias', 'foo-alias'))
Expand Down

0 comments on commit 76ba2c6

Please sign in to comment.