Problem Statement
The project currently uses custom string-based email validation logic that only checks for the presence of "@" and "." characters.
Example:
if "@" not in v or "." not in v.split("@")[-1]:
raise ValueError("Invalid email address")
This approach can incorrectly accept invalid email addresses and may reject some valid formats. It also duplicates functionality already provided by Pydantic.
Proposed Solution
Replace the custom email validation logic with Pydantic's built-in EmailStr type.
Example:
from pydantic import EmailStr
class SubscribeRequest(BaseModel):
email: EmailStr
Benefits:
- More accurate email validation.
- Reduced custom validation code.
- Better maintainability.
- Leverages a well-tested Pydantic feature.
- Consistent validation behavior across the application.
Alternatives Considered
-
Improve the existing custom validation logic using regular expressions.
- More complex to maintain.
- Still less reliable than Pydantic's built-in validation.
-
Use a third-party email validation library directly.
- Adds unnecessary complexity since Pydantic already provides this functionality.
Additional Context
The current validation implementation exists in request schemas and performs only basic string checks. Migrating to EmailStr would simplify the codebase and improve validation accuracy while keeping the API behavior consistent.
This change is beginner-friendly and has a small implementation scope, making it a good first contribution.
Problem Statement
The project currently uses custom string-based email validation logic that only checks for the presence of "@" and "." characters.
Example:
This approach can incorrectly accept invalid email addresses and may reject some valid formats. It also duplicates functionality already provided by Pydantic.
Proposed Solution
Replace the custom email validation logic with Pydantic's built-in
EmailStrtype.Example:
Benefits:
Alternatives Considered
Improve the existing custom validation logic using regular expressions.
Use a third-party email validation library directly.
Additional Context
The current validation implementation exists in request schemas and performs only basic string checks. Migrating to
EmailStrwould simplify the codebase and improve validation accuracy while keeping the API behavior consistent.This change is beginner-friendly and has a small implementation scope, making it a good first contribution.