Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EmailValidator #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
SetValidator,
UniqueValidator,
Validator,
EmailValidator,
_stringify_set,
)

Expand Down Expand Up @@ -52,12 +53,21 @@ def _raise_valueerror(x):
def test_float_validator_works(field):
FloatValidator().validate(field)


@pytest.mark.parametrize("field", [("foo"), (" "), ("7..0"), ("4,200")])
def test_float_validator_fails(field):
with pytest.raises(ValidationException):
FloatValidator().validate(field)


@pytest.mark.parametrize("field", [("adonis@mail.com"), ("nom@mail.co.ng")])
def test_email_validator_works(field):
EmailValidator().validate(field)

@pytest.mark.parametrize("field", [("adonis@@mail.com"), ("nom_mail.co.ng")])
def test_email_validator_fails(field):
with pytest.raises(ValidationException):
EmailValidator().validate(field)


@pytest.mark.parametrize("field", [("42")])
def test_int_validator_works(field):
Expand Down
14 changes: 14 additions & 0 deletions vladiate/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ def validate(self, field, row={}):
def bad(self):
return self.failed


class EmailValidator(Validator):
email = None
_email_regex = '[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'

def validate(self, field, row):
self.email = field
if not bool(re.match(self._email_regex, field)):
raise ValidationException(' {} is not a valid email'.format(field))

@property
def bad(self):
return self.email


class Ignore(Validator):
""" Ignore a given field. Never fails """
Expand Down