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
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for an email address value object."""
3from dataclasses import dataclass
5from email_validator import validate_email
8class InvalidEmailException(Exception):
9 """Raised when the email address is not valid."""
12@dataclass(frozen=True, slots=True)
13class EmailAddress:
14 """A value object for an email address."""
16 email: str
18 def __post_init__(self):
19 """Check if the email address is valid."""
20 from email_validator import EmailNotValidError
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
29 def __str__(self):
30 """Return the string representation of an email address."""
31 return self.email