diff options
author | epenet <6771947+epenet@users.noreply.github.com> | 2022-04-01 09:01:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-01 09:01:52 +0200 |
commit | 1d6d41a5e44a7abfc11ced0601a8fb788e655b03 (patch) | |
tree | fe3c3b7da8a213db9a2b481cb3c2b9c20398fc1a | |
parent | c3d61efc75429bdd30213878b416722ca164c828 (diff) | |
download | voluptuous-1d6d41a5e44a7abfc11ced0601a8fb788e655b03.tar.gz |
Fix email regex match for Python 2.7 (#456)
-rw-r--r-- | voluptuous/tests/tests.py | 12 | ||||
-rw-r--r-- | voluptuous/validators.py | 14 |
2 files changed, 23 insertions, 3 deletions
diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index d87f301..049221b 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -245,6 +245,18 @@ def test_email_validation_without_host(): else: assert False, "Did not raise Invalid for empty string URL" +def test_email_validation_with_bad_data(): + """ Test with bad data in email address """ + schema = Schema({"email": Email()}) + for email in ('john@voluptuous.com>', 'john!@voluptuous.org!@($*!'): + try: + schema({"email": 'john@voluptuous.com>'}) + except MultipleInvalid as e: + assert_equal(str(e), + "expected an email address for dictionary value @ data['email']") + else: + assert False, "Did not raise Invalid for bad email " + email + def test_fqdn_url_validation(): """ Test with valid fully qualified domain name URL """ diff --git a/voluptuous/validators.py b/voluptuous/validators.py index 972fdde..109342d 100644 --- a/voluptuous/validators.py +++ b/voluptuous/validators.py @@ -22,21 +22,29 @@ else: # Taken from https://github.com/kvesteri/validators/blob/master/validators/email.py USER_REGEX = re.compile( + # start anchor, because fullmatch is not available in python 2.7 + "(?:" # dot-atom r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+" r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$" # quoted-string r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|' - r"""\\[\001-\011\013\014\016-\177])*"$)""", + r"""\\[\001-\011\013\014\016-\177])*"$)""" + # end anchor, because fullmatch is not available in python 2.7 + r")\Z", re.IGNORECASE ) DOMAIN_REGEX = re.compile( + # start anchor, because fullmatch is not available in python 2.7 + "(?:" # domain r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+' r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?$)' # literal form, ipv4 address (SMTP 4.1.3) r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)' - r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', + r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$' + # end anchor, because fullmatch is not available in python 2.7 + r")\Z", re.IGNORECASE) __author__ = 'tusharmakkar08' @@ -438,7 +446,7 @@ def Email(v): raise EmailInvalid("Invalid email address") user_part, domain_part = v.rsplit('@', 1) - if not (USER_REGEX.fullmatch(user_part) and DOMAIN_REGEX.fullmatch(domain_part)): + if not (USER_REGEX.match(user_part) and DOMAIN_REGEX.match(domain_part)): raise EmailInvalid("Invalid email address") return v except: |