Skip to content

TLD list testing added #282

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

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ include = ["CHANGES.md", "docs/*", "docs/validators.1", "validators/py.typed"]
[tool.poetry.dependencies]
python = "^3.8"
eth-hash = {extras = ["pycryptodome"], version = "^0.5.2"}
requests = "^2.31.0"

[tool.poetry.group.docs]
optional = true
Expand Down
2 changes: 2 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def test_returns_true_on_valid_domain(value: str, rfc_1034: bool, rfc_2782: bool
("123.123", False, False),
("123.123.123.", True, False),
("123.123.123.123", False, False),
("sanpellegrino-corporate.itOLDWEBSITE", False, False),
("sanpellegrino-corporate.itOLDWEBSITE.", True, False),
],
)
def test_returns_failed_validation_on_invalid_domain(value: str, rfc_1034: bool, rfc_2782: bool):
Expand Down
32 changes: 30 additions & 2 deletions validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@

# standard
import re
import requests

# local
from .utils import validator
from validators.utils import validator


# Function to download the TLD list and create a set of valid TLDs
def get_valid_tlds():
"""Return a set of regularly updated valid TLDs from inaa.org ."""
response = requests.get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt", timeout=30)
if response.status_code != 200:
return None
tlds = response.text.strip().split("\n")[1:]
return tlds


VALID_TLDS = get_valid_tlds()


@validator
Expand Down Expand Up @@ -42,11 +56,25 @@ def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):
- *In version 0.10.0*:
- Added support for internationalized domain name (IDN) validation.

> *New in version 0.9.0*.
- *In version 0.21.0*:
- Added active TLD validation.

> *New in version 0.21.0*.
"""
if not value:
return False
try:
# Check if the TLD is active
if rfc_1034 and value.endswith("."):
tld = value.rstrip(".")
_, tld = tld.rsplit(".", 1)
else:
_, tld = value.rsplit(".", 1)

if VALID_TLDS:
if tld.upper() not in VALID_TLDS:
return False

return not re.search(r"\s", value) and re.match(
# First character of the domain
rf"^(?:[a-zA-Z0-9{'_'if rfc_2782 else ''}]"
Expand Down
4 changes: 2 additions & 2 deletions validators/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import re

# local
from .hostname import hostname
from .utils import validator
from validators.hostname import hostname
from validators.utils import validator


@validator
Expand Down