Coverage for src/kwai/core/domain/value_objects/email_address.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module for an email address value object.""" 

2 

3from dataclasses import dataclass 

4 

5from email_validator import validate_email 

6 

7 

8class InvalidEmailException(Exception): 

9 """Raised when the email address is not valid.""" 

10 

11 

12@dataclass(frozen=True, slots=True) 

13class EmailAddress: 

14 """A value object for an email address.""" 

15 

16 email: str 

17 

18 def __post_init__(self): 

19 """Check if the email address is valid.""" 

20 from email_validator import EmailNotValidError 

21 

22 try: 

23 validate_email(self.email, check_deliverability=False) 

24 except EmailNotValidError as exc: 

25 raise InvalidEmailException( 

26 f"{self.email} is not a valid email address: {exc}" 

27 ) from exc 

28 

29 def __str__(self): 

30 """Return the string representation of an email address.""" 

31 return self.email