-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename the
email
field of ValidatedEmail to normalized
to be clea…
…rer about its importance
- Loading branch information
Showing
6 changed files
with
56 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,11 +65,11 @@ try: | |
# Check that the email address is valid. Turn on check_deliverability | ||
# for first-time validations like on account creation pages (but not | ||
# login pages). | ||
validation = validate_email(email, check_deliverability=False) | ||
emailinfo = validate_email(email, check_deliverability=False) | ||
|
||
# After this point, use only the normalized form of the email address, | ||
# especially before going to a database query. | ||
email = validation.email | ||
email = emailinfo.normalized | ||
|
||
except EmailNotValidError as e: | ||
|
||
|
@@ -158,7 +158,7 @@ from email_validator import validate_email, caching_resolver | |
resolver = caching_resolver(timeout=10) | ||
|
||
while True: | ||
email = validate_email(email, dns_resolver=resolver).email | ||
validate_email(email, dns_resolver=resolver) | ||
``` | ||
|
||
### Test addresses | ||
|
@@ -248,8 +248,8 @@ This library gives you back the ASCII-ized form in the `ascii_email` | |
field in the returned object, which you can get like this: | ||
|
||
```python | ||
valid = validate_email(email, allow_smtputf8=False) | ||
email = valid.ascii_email | ||
emailinfo = validate_email(email, allow_smtputf8=False) | ||
email = emailinfo.ascii_email | ||
``` | ||
|
||
The local part is left alone (if it has internationalized characters | ||
|
@@ -274,9 +274,9 @@ equivalent in domain names to their ASCII counterparts. This library | |
normalizes them to their ASCII counterparts: | ||
|
||
```python | ||
valid = validate_email("me@Domain.com") | ||
print(valid.email) | ||
print(valid.ascii_email) | ||
emailinfo = validate_email("me@Domain.com") | ||
print(emailinfo.normalized) | ||
print(emailinfo.ascii_email) | ||
# prints "[email protected]" twice | ||
``` | ||
|
||
|
@@ -320,7 +320,7 @@ For the email address `[email protected]`, the returned object is: | |
|
||
```python | ||
ValidatedEmail( | ||
email='[email protected]', | ||
normalized='[email protected]', | ||
local_part='test', | ||
domain='joshdata.me', | ||
ascii_email='[email protected]', | ||
|
@@ -334,7 +334,7 @@ internationalized domain but ASCII local part, the returned object is: | |
|
||
```python | ||
ValidatedEmail( | ||
email='example@ツ.life', | ||
normalized='example@ツ.life', | ||
local_part='example', | ||
domain='ツ.life', | ||
ascii_email='[email protected]', | ||
|
@@ -357,7 +357,7 @@ internationalized local part, the returned object is: | |
|
||
```python | ||
ValidatedEmail( | ||
email='ツ[email protected]', | ||
normalized='ツ[email protected]', | ||
local_part='ツ-test', | ||
domain='joshdata.me', | ||
ascii_email=None, | ||
|
@@ -380,8 +380,8 @@ are: | |
|
||
| Field | Value | | ||
| -----:|-------| | ||
| `email` | The normalized form of the email address that you should put in your database. This combines the `local_part` and `domain` fields (see below). | | ||
| `ascii_email` | If set, an ASCII-only form of the email address by replacing the domain part with [IDNA](https://tools.ietf.org/html/rfc5891) [Punycode](https://www.rfc-editor.org/rfc/rfc3492.txt). This field will be present when an ASCII-only form of the email address exists (including if the email address is already ASCII). If the local part of the email address contains internationalized characters, `ascii_email` will be `None`. If set, it merely combines `ascii_local_part` and `ascii_domain`. | | ||
| `normalized` | The normalized form of the email address that you should put in your database. This combines the `local_part` and `domain` fields (see below). | | ||
| `ascii_email` | If set, an ASCII-only form of the normalized email address by replacing the domain part with [IDNA](https://tools.ietf.org/html/rfc5891) [Punycode](https://www.rfc-editor.org/rfc/rfc3492.txt). This field will be present when an ASCII-only form of the email address exists (including if the email address is already ASCII). If the local part of the email address contains internationalized characters, `ascii_email` will be `None`. If set, it merely combines `ascii_local_part` and `ascii_domain`. | | ||
| `local_part` | The normalized local part of the given email address (before the @-sign). Normalization includes Unicode NFC normalization and removing unnecessary quoted-string quotes and backslashes. If `allow_quoted_local` is True and the surrounding quotes are necessary, the quotes _will_ be present in this field. | | ||
| `ascii_local_part` | If set, the local part, which is composed of ASCII characters only. | | ||
| `domain` | The canonical internationalized Unicode form of the domain part of the email address. If the returned string contains non-ASCII characters, either the [SMTPUTF8](https://tools.ietf.org/html/rfc6531) feature of your mail relay will be required to transmit the message or else the email address's domain part must be converted to IDNA ASCII first: Use `ascii_domain` field instead. | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ def test_dict_accessor(): | |
input_email = "[email protected]" | ||
valid_email = validate_email(input_email, check_deliverability=False) | ||
assert isinstance(valid_email.as_dict(), dict) | ||
assert valid_email.as_dict()["original_email"] == input_email | ||
assert valid_email.as_dict()["original"] == input_email | ||
|
||
|
||
def test_main_single_good_input(monkeypatch, capsys): | ||
|
@@ -24,7 +24,7 @@ def test_main_single_good_input(monkeypatch, capsys): | |
stdout, _ = capsys.readouterr() | ||
output = json.loads(str(stdout)) | ||
assert isinstance(output, dict) | ||
assert validate_email(test_email, dns_resolver=RESOLVER).original_email == output["original_email"] | ||
assert validate_email(test_email, dns_resolver=RESOLVER).original == output["original"] | ||
|
||
|
||
def test_main_single_bad_input(monkeypatch, capsys): | ||
|
@@ -53,7 +53,7 @@ def test_bytes_input(): | |
input_email = b"[email protected]" | ||
valid_email = validate_email(input_email, check_deliverability=False) | ||
assert isinstance(valid_email.as_dict(), dict) | ||
assert valid_email.as_dict()["email"] == input_email.decode("utf8") | ||
assert valid_email.as_dict()["normalized"] == input_email.decode("utf8") | ||
|
||
input_email = "testaddr中example.tld".encode("utf32") | ||
with pytest.raises(EmailSyntaxError): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
smtputf8=False, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='[email protected]', | ||
normalized='[email protected]', | ||
ascii_email='[email protected]', | ||
), | ||
), | ||
|
@@ -28,7 +28,7 @@ | |
smtputf8=False, | ||
ascii_domain='test-example.com', | ||
domain='test-example.com', | ||
email='[email protected]', | ||
normalized='[email protected]', | ||
ascii_email='[email protected]', | ||
), | ||
), | ||
|
@@ -40,7 +40,7 @@ | |
smtputf8=False, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='user+mailbox/[email protected]', | ||
normalized='user+mailbox/[email protected]', | ||
ascii_email='user+mailbox/[email protected]', | ||
), | ||
), | ||
|
@@ -52,7 +52,7 @@ | |
smtputf8=False, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email="!#$%&'*+-/=?^_`.{|}[email protected]", | ||
normalized="!#$%&'*+-/=?^_`.{|}[email protected]", | ||
ascii_email="!#$%&'*+-/=?^_`.{|}[email protected]", | ||
), | ||
), | ||
|
@@ -64,7 +64,7 @@ | |
smtputf8=False, | ||
ascii_domain='xn--fiqq24b10vi0d.tw', | ||
domain='臺網中心.tw', | ||
email='jeff@臺網中心.tw', | ||
normalized='jeff@臺網中心.tw', | ||
ascii_email='[email protected]', | ||
), | ||
), | ||
|
@@ -88,7 +88,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--5nqv22n.xn--lhr59c', | ||
domain='郵件.商務', | ||
email='伊昭傑@郵件.商務', | ||
normalized='伊昭傑@郵件.商務', | ||
), | ||
), | ||
( | ||
|
@@ -98,7 +98,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--l2bl7a9d.xn--o1b8dj2ki', | ||
domain='मोहन.ईन्फो', | ||
email='राम@मोहन.ईन्फो', | ||
normalized='राम@मोहन.ईन्फो', | ||
), | ||
), | ||
( | ||
|
@@ -108,7 +108,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--80ajglhfv.xn--j1aef', | ||
domain='екзампл.ком', | ||
email='юзер@екзампл.ком', | ||
normalized='юзер@екзампл.ком', | ||
), | ||
), | ||
( | ||
|
@@ -118,7 +118,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--mxahbxey0c.xn--xxaf0a', | ||
domain='εχαμπλε.ψομ', | ||
email='θσερ@εχαμπλε.ψομ', | ||
normalized='θσερ@εχαμπλε.ψομ', | ||
), | ||
), | ||
( | ||
|
@@ -128,7 +128,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--fiqq24b10vi0d.tw', | ||
domain='臺網中心.tw', | ||
email='葉士豪@臺網中心.tw', | ||
normalized='葉士豪@臺網中心.tw', | ||
), | ||
), | ||
( | ||
|
@@ -138,7 +138,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--fiqq24b10vi0d.xn--kpry57d', | ||
domain='臺網中心.台灣', | ||
email='葉士豪@臺網中心.台灣', | ||
normalized='葉士豪@臺網中心.台灣', | ||
), | ||
), | ||
( | ||
|
@@ -148,7 +148,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--fiqq24b10vi0d.tw', | ||
domain='臺網中心.tw', | ||
email='jeff葉@臺網中心.tw', | ||
normalized='jeff葉@臺網中心.tw', | ||
), | ||
), | ||
( | ||
|
@@ -158,7 +158,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='ñoñó@example.tld', | ||
normalized='ñoñó@example.tld', | ||
), | ||
), | ||
( | ||
|
@@ -168,7 +168,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='我買@example.tld', | ||
normalized='我買@example.tld', | ||
), | ||
), | ||
( | ||
|
@@ -178,7 +178,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='甲斐黒川日本@example.tld', | ||
normalized='甲斐黒川日本@example.tld', | ||
), | ||
), | ||
( | ||
|
@@ -188,7 +188,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='example.tld', | ||
domain='example.tld', | ||
email='чебурашкаящик-с-апельсинами.рф@example.tld', | ||
normalized='чебурашкаящик-с-апельсинами.рф@example.tld', | ||
), | ||
), | ||
( | ||
|
@@ -198,7 +198,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='domain.with.idn.tld', | ||
domain='domain.with.idn.tld', | ||
email='उदाहरण.परीक्ष@domain.with.idn.tld', | ||
normalized='उदाहरण.परीक्ष@domain.with.idn.tld', | ||
), | ||
), | ||
( | ||
|
@@ -208,7 +208,7 @@ def test_email_valid(email_input, output): | |
smtputf8=True, | ||
ascii_domain='xn--qxaa9ba.gr', | ||
domain='εεττ.gr', | ||
email='ιωάννης@εεττ.gr', | ||
normalized='ιωάννης@εεττ.gr', | ||
), | ||
), | ||
], | ||
|