Skip to content
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
25 changes: 16 additions & 9 deletions src/validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,21 @@ def domain(
if not value:
return False

if consider_tld and not _IanaTLD.check(value.rstrip(".").rsplit(".", 1)[-1].upper()):
return False

try:
ascii_domain = value.encode("idna").decode("utf-8")
domain_without_trailing_dot = ascii_domain.rstrip(".")

if not domain_without_trailing_dot or len(domain_without_trailing_dot) > 253:
return False

if consider_tld and not _IanaTLD.check(
domain_without_trailing_dot.rsplit(".", 1)[-1].upper()
):
return False

service_record = r"_" if rfc_2782 else ""
trailing_dot = r"\.?$" if rfc_1034 else r"$"
tld = r"(?:[a-z]{2,63}|xn--[a-z0-9](?:[a-z0-9-]{0,57}[a-z0-9])?)"

return not re.search(r"\s|__+", value) and re.match(
# First character of the domain
Expand All @@ -90,12 +99,10 @@ def domain(
+ rf"(?:[a-z0-9-{service_record}]{{0,61}}"
# Hostname
+ rf"[a-z0-9{service_record}])?\.)"
# First 61 characters of the gTLD
+ r"+[a-z0-9][a-z0-9-_]{0,61}"
# Last character of the gTLD
+ rf"[a-z]{trailing_dot}",
value.encode("idna").decode("utf-8"),
# Top-level domain
+ rf"+{tld}{trailing_dot}",
ascii_domain,
re.IGNORECASE,
)
except UnicodeError as err:
raise UnicodeError(f"Unable to encode/decode {value}") from err
raise UnicodeError(f"Unable to encode/decode {value}") from err
5 changes: 4 additions & 1 deletion tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def test_returns_true_on_valid_top_level_domain(
("123.123", False, False),
("123.123.123.", True, False),
("123.123.123.123", False, False),
("example.c_m", False, False),
("example.c-m", False, False),
(("a." * 200) + "com", False, False),
],
)
def test_returns_failed_validation_on_invalid_domain(value: str, rfc_1034: bool, rfc_2782: bool):
Expand Down Expand Up @@ -106,4 +109,4 @@ def test_returns_failed_validation_invalid_top_level_domain(
assert isinstance(
domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782),
ValidationError,
)
)
5 changes: 4 additions & 1 deletion tests/test_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ def test_returns_true_on_valid_hostname(value: str, rfc_1034: bool, rfc_2782: bo
("[dead:beef:0:-:0:-:42:1]:5731", False, False),
("[0:0:0:0:0:ffff:1.2.3.4]:-65538", False, False),
("[0:&:b:c:@:e:f:::9999", False, False),
("example.c_m", False, False),
("example.c-m", False, False),
(("a." * 200) + "com", False, False),
],
)
def test_returns_failed_validation_on_invalid_hostname(value: str, rfc_1034: bool, rfc_2782: bool):
"""Test returns failed validation on invalid hostname."""
assert isinstance(hostname(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)
assert isinstance(hostname(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)