Fix email validation dependency issue

- Add email-validator to requirements.txt
- Add fallback email validation for user and supplier schemas
- Implement graceful handling when email-validator is not installed
This commit is contained in:
Automated Action 2025-06-17 19:05:26 +00:00
parent a17fe518a9
commit 5da770418f
3 changed files with 52 additions and 5 deletions

View File

@ -1,16 +1,39 @@
import re
from typing import Optional
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel, validator
# Try to import EmailStr, but fallback to str if email-validator is not installed
try:
from pydantic import EmailStr
email_type = EmailStr
except ImportError:
email_type = str
# Shared properties
class SupplierBase(BaseModel):
name: Optional[str] = None
contact_name: Optional[str] = None
email: Optional[EmailStr] = None
email: Optional[email_type] = None
phone: Optional[str] = None
address: Optional[str] = None
website: Optional[str] = None
notes: Optional[str] = None
# Add email validation if using fallback str type
@validator('email')
def validate_email(cls, v):
if v is None:
return v
if not isinstance(v, str):
return v
# Basic email validation pattern
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(pattern, v):
raise ValueError('Invalid email format')
return v
# Properties to receive on supplier creation

View File

@ -1,18 +1,41 @@
import re
from typing import Optional
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel, validator
# Try to import EmailStr, but fallback to str if email-validator is not installed
try:
from pydantic import EmailStr
email_type = EmailStr
except ImportError:
email_type = str
# Shared properties
class UserBase(BaseModel):
email: Optional[EmailStr] = None
email: Optional[email_type] = None
is_active: Optional[bool] = True
is_superuser: bool = False
full_name: Optional[str] = None
# Add email validation if using fallback str type
@validator('email')
def validate_email(cls, v):
if v is None:
return v
if not isinstance(v, str):
return v
# Basic email validation pattern
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(pattern, v):
raise ValueError('Invalid email format')
return v
# Properties to receive via API on creation
class UserCreate(UserBase):
email: EmailStr
email: email_type
password: str

View File

@ -7,5 +7,6 @@ pydantic-settings>=2.0.3
python-jose[cryptography]>=3.3.0
passlib[bcrypt]>=1.7.4
python-multipart>=0.0.6
email-validator>=2.0.0
ruff>=0.1.3
pytest>=7.4.3