ac-user-auth / app /models /auth_models.py
MukeshKapoor25's picture
first commit
4438ee0
from pydantic import BaseModel, EmailStr, Field, validator
import phonenumbers
from pydantic import BaseModel, EmailStr, Field, validator
class RegisterUser(BaseModel):
email: EmailStr = Field(..., description="User's email address")
mobile: str = Field(..., description="User's mobile number in international format (e.g., +14155552671)")
name: str = Field(..., min_length=1, description="User's full name")
@validator("mobile")
def validate_mobile(cls, value):
"""
Validate the mobile number format using the phonenumbers library.
"""
try:
parsed_number = phonenumbers.parse(value)
if not phonenumbers.is_valid_number(parsed_number):
raise ValueError("The mobile number is not valid. Please use international format (e.g., +14155552671).")
except Exception:
raise ValueError("The mobile number is not valid. Please use international format (e.g., +14155552671).")
return value
@validator("email")
def normalize_email(cls, value):
"""
Normalize email to lowercase.
"""
return value.strip().lower()
@validator("name")
def normalize_name(cls, value):
"""
Normalize name by trimming whitespace and capitalizing each word.
"""
return " ".join(word.capitalize() for word in value.strip().split())