from functools import partial
from typing import Annotated
from pydantic import AfterValidator, BaseModel
def validate_smth(value: str, context: str) -> str:
if value == "test":
print(f"Validated {value}")
else:
raise ValueError(f"Value {value} is not allowed in context {context}")
return value
class MyModel(BaseModel):
field: Annotated[str, AfterValidator(partial(validate_smth, context="test"))]
MyModel.model_validate({"field": "test"})