You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This discussion explores the unexpected behavior of pydantic.EmailStr when used for email validation in FastAPI. While the validation works as expected in REST API endpoints, it seems to be bypassed in Strawberry GraphQL mutations.
Here's a breakdown of the observed behavior:
REST API
A UserModel with EmailStr for the email field ensures that invalid email addresses trigger validation errors in the response.
Strawberry GraphQL
A UserInput type with a field corresponding to the email might not enforce validation using EmailStr. The provided code snippet doesn't explicitly validate the email before user creation.
Possible Reasons:
Missing Validation in GraphQL Mutation: The Strawberry mutation code might be missing the logic to leverage Pydantic's validation for EmailStr.
Strawberry Decorator Behavior: There could be subtle differences in how Strawberry handles data types compared to FastAPI's default behavior.
Discussion Points:
How to effectively utilize pydantic.EmailStr for email validation in Strawberry GraphQL mutations?
Are there specific decorators or techniques needed to ensure Pydantic validation is triggered within Strawberry?
Should additional validation logic be implemented within the mutation itself?
Code Snippets:
REST
# DTOclassUserModel(BaseModel):
name: stremail: EmailStrpassword: strpassword_confirm: str# route@core_routes.post("/users")asyncdefcreate_user(input: UserModel):
user=awaitUser.create(**input.model_dump())
returnGetUser.from_queryset_single(User.get(id=user.id))
# API JSON supplied inputs
{
"name": "string",
"email": "user",
"password": "string",
"password_confirm": "string"
}
# Response with validation error
{
"detail": [
{
"type": "value_error",
"loc": [
"body",
"email"
],
"msg": "value is not a valid email address: The email address is not valid. It must have exactly one @-sign.",
"input": "user",
"ctx": {
"reason": "The email address is not valid. It must have exactly one @-sign."
}
}
]
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Description
This discussion explores the unexpected behavior of
pydantic.EmailStr
when used for email validation in FastAPI. While the validation works as expected in REST API endpoints, it seems to be bypassed in Strawberry GraphQL mutations.Here's a breakdown of the observed behavior:
REST API
UserModel
withEmailStr
for theemail
field ensures that invalid email addresses trigger validation errors in the response.Strawberry GraphQL
UserInput
type with a field corresponding to the email might not enforce validation usingEmailStr
. The provided code snippet doesn't explicitly validate the email before user creation.Possible Reasons:
EmailStr
.Discussion Points:
pydantic.EmailStr
for email validation in Strawberry GraphQL mutations?Code Snippets:
Stacks:
Beta Was this translation helpful? Give feedback.
All reactions