from tortoise.exceptions import ValidationError
from tortoise.validators import Validator

from service.core.exceptions import InvalidEmail
from service.utils.validation import validate_email


class EmailValidator(Validator):
    def __init__(self, use_dns: bool = False):
        self.use_dns = use_dns

    def __call__(self, value: str) -> None:
        try:
            validate_email(value, self.use_dns)
        except InvalidEmail as e:
            raise ValidationError(str(e))
