Skip to content

Commit e23e39a

Browse files
committed
fix: add EmailStr validation to base schema
Change email field from str to EmailStr for proper email validation at the schema level. This provides consistent validation across all user schemas that inherit from UserBase. Also updates test to expect 422 (Pydantic validation) instead of 400 (application validation) for invalid email addresses.
1 parent 1c39ffa commit e23e39a

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

app/schemas/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Define the Base Schema structures we will inherit from."""
22

3-
from pydantic import BaseModel, ConfigDict, Field
3+
from pydantic import BaseModel, ConfigDict, EmailStr, Field
44

55
from app.schemas.examples import ExampleUser
66

@@ -10,4 +10,4 @@ class UserBase(BaseModel):
1010

1111
model_config = ConfigDict(from_attributes=True)
1212

13-
email: str = Field(examples=[ExampleUser.email])
13+
email: EmailStr = Field(examples=[ExampleUser.email])

tests/integration/test_auth_routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ async def test_register_new_user_with_bad_email(
162162
},
163163
)
164164

165-
assert response.status_code == status.HTTP_400_BAD_REQUEST
166-
assert response.json()["detail"] == "This email address is not valid"
165+
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
166+
# EmailStr validation provides a standard Pydantic error message
167167

168168
user_from_db = await test_db.get(User, 1)
169169
assert user_from_db is None

0 commit comments

Comments
 (0)