Skip to content

Commit

Permalink
chore: update dependencies for support pydantic V2 and adapt code in …
Browse files Browse the repository at this point in the history
…consequences (#195)
  • Loading branch information
fchancel authored Jul 18, 2023
1 parent fab70e4 commit c15d24e
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 440 deletions.
2 changes: 1 addition & 1 deletion fastapi_mail/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from aiosmtplib.api import DEFAULT_TIMEOUT
from jinja2 import Environment, FileSystemLoader
from pydantic import BaseSettings as Settings
from pydantic import DirectoryPath, EmailStr, conint
from pydantic_settings import BaseSettings as Settings


class ConnectionConfig(Settings):
Expand Down
19 changes: 13 additions & 6 deletions fastapi_mail/email_utils/email_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import dns.exception
import dns.resolver
from email_validator import EmailNotValidError, validate_email

try:
from redis import asyncio as aioredis
Expand All @@ -19,8 +20,6 @@
except ImportError:
request_lib = False

from pydantic import EmailStr

from fastapi_mail.errors import ApiError, DBProvaiderError


Expand Down Expand Up @@ -168,7 +167,11 @@ async def init_redis(self) -> bool:

def validate_email(self, email: str) -> bool:
"""Validate email address"""
EmailStr.validate(email)
try:
emailinfo = validate_email(email, check_deliverability=False)
email = emailinfo.normalized
except EmailNotValidError:
raise EmailNotValidError
return True

async def fetch_temp_email_domains(self) -> Union[List[str], Any]:
Expand Down Expand Up @@ -370,9 +373,13 @@ async def fetch_info(self) -> bool:

def validate_email(self, email: str) -> bool:
"""Validate email address"""
if EmailStr.validate(email):
return True
return False

try:
emailinfo = validate_email(email, check_deliverability=False)
email = emailinfo.normalized
except EmailNotValidError:
return False
return True

def catch_all_check(self) -> bool:
"""
Expand Down
15 changes: 7 additions & 8 deletions fastapi_mail/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mimetypes import MimeTypes
from typing import Dict, List, Optional, Union

from pydantic import BaseModel, EmailStr, root_validator, validator
from pydantic import BaseModel, ConfigDict, EmailStr, field_validator, model_validator
from starlette.datastructures import Headers, UploadFile

from fastapi_mail.errors import WrongFile
Expand Down Expand Up @@ -48,7 +48,7 @@ class MessageSchema(BaseModel):
multipart_subtype: MultipartSubtypeEnum = MultipartSubtypeEnum.mixed
headers: Optional[Dict] = None

@validator("attachments")
@field_validator("attachments")
def validate_file(cls, v):
temp = []
mime = MimeTypes()
Expand Down Expand Up @@ -88,17 +88,16 @@ def validate_file(cls, v):
)
return temp

@root_validator
@model_validator(mode="after")
def validate_alternative_body(cls, values):
"""
Validate alternative_body field
"""
if (
values["multipart_subtype"] != MultipartSubtypeEnum.alternative
and values["alternative_body"]
values.multipart_subtype != MultipartSubtypeEnum.alternative
and values.alternative_body
):
values["alternative_body"] = None
values.alternative_body = None
return values

class Config:
arbitrary_types_allowed = True
model_config = ConfigDict(arbitrary_types_allowed=True)
Loading

0 comments on commit c15d24e

Please sign in to comment.