-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
32 lines (24 loc) · 941 Bytes
/
classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Any
from django.core.exceptions import ValidationError # type: ignore
from django.core.validators import EmailValidator as DjangoEmailValidator # type: ignore
from validx import Str, exc
class Email(Str):
def __init__(self, *args, email_validator=DjangoEmailValidator, **kwargs):
super(Email, self).__init__(*args, **kwargs)
self.email_validator = email_validator
def __call__(self, value: Any, __context=None) -> Any:
"""subclass __call__ to add validator checking.
"""
value = super(Email, self).__call__(
value,
__context=__context,
)
if self.email_validator:
try:
self.email_validator()(value)
except ValidationError:
raise exc.PatternMatchError(
expected='valid email',
actual=value,
)
return value