🏷️ Allow `AliasChoices` and `AliasPath` for `validation_alias` in `Field` by YuriiMotov · Pull Request #1728 · fastapi/sqlmodel
This PR updates type hints for validation_alias parameter of Field to be in line with Pydantic's version:
validation_alias: str | AliasPath | AliasChoices | None = _Unset,
Also, added tests for AliasPath and AliasChoices
The following code works on master:
from typing import Optional from pydantic import AliasChoices, AliasPath from sqlmodel import Field, SQLModel class User(SQLModel): first_name: str = Field(validation_alias="firstName", serialization_alias="f_name") second_name: Optional[str] = Field( default=None, validation_alias=AliasChoices("secondName", "surname") ) nickname: Optional[str] = Field( default=None, validation_alias=AliasPath("names", 2) ) test_data = [ {"firstName": "John", "secondName": "Doe", "names": ["John", "Doe", "Johnny"]}, {"firstName": "John", "surname": "Doe", "names": ["John", "Doe", "Johnny"]}, ] for data in test_data: print(User.model_validate(data)) # first_name='John' second_name='Doe' nickname='Johnny' # first_name='John' second_name='Doe' nickname='Johnny'
.. but mypy argues with:
main.py:9: error: No overload variant of "Field" matches argument types "None", "AliasChoices" [call-overload]
main.py:12: error: No overload variant of "Field" matches argument types "None", "AliasPath" [call-overload]
This PR fixes this.