Skip to content

Replace custom email validation with Pydantic EmailStr #971

Description

@Ommdutta26

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

  1. Improve the existing custom validation logic using regular expressions.

    • More complex to maintain.
    • Still less reliable than Pydantic's built-in validation.
  2. 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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions