Source code for src.share_kernel.domain.value_objects.address
from dataclasses import dataclass
from src.share_kernel.domain.base_value_object import ValueObject
from src.share_kernel.domain.exceptions import InvalidValueObjectException
[docs]
@dataclass(frozen=True)
class Address(ValueObject):
street: str
city: str
postal_code: str
country_code: str # ISO 3166-1 alpha-2 (ex: "FR", "MA", "US")
state_or_region: str = "" # optionnel (provinces, départements…)
def _validate(self):
if not self.street.strip():
raise InvalidValueObjectException("L'adresse doit avoir une rue.")
if not self.city.strip():
raise InvalidValueObjectException("L'adresse doit avoir une ville.")
if not self.postal_code.strip():
raise InvalidValueObjectException("L'adresse doit avoir un code postal.")
if len(self.country_code) != 2 or not self.country_code.isalpha():
raise InvalidValueObjectException(
f"country_code doit être un code ISO 3166-1 alpha-2 (ex: 'FR'). "
f"Reçu : '{self.country_code}'"
)
def __str__(self) -> str:
parts = [self.street, self.postal_code, self.city]
if self.state_or_region:
parts.append(self.state_or_region)
parts.append(self.country_code.upper())
return ", ".join(parts)