EmailValidator

Documentation for the `EmailValidator` class.

Namespace blazingapi.orm.validators

The EmailValidator class is a validator that checks if a field is a valid email address.

How to Use

from blazingapi.orm.models import Model
from blazingapi.orm.fields import VarCharField
from blazingapi.orm.validators import EmailValidator


class Author(Model):
    email = VarCharField(max_length=256, validators=[EmailValidator()])

Class Definition

import re


class EmailValidator:
    email_regex = re.compile(
        r"(^[-!#$%&'*+/=?^_`{|}~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{|}~0-9A-Z]+)*"
        r'|^"([]!#-[^-~ \t]|(\\[\t -~]))+")@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE
    )

    def __call__(self, value):
        if value and not self.email_regex.match(value):
            raise ValueError(f"Invalid email address: {value}")