Skip to content

Commit

Permalink
fix: rfc cases in the domain validator
Browse files Browse the repository at this point in the history
  • Loading branch information
yozachar committed Apr 18, 2024
1 parent f04a8c5 commit 181f6dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ def domain(
return False

try:
return not re.search(r"\s", value) and re.match(

service_record = r"_" if rfc_2782 else ""
trailing_dot = r"\.?$" if rfc_1034 else r"$"

return not re.search(r"\s|__+", value) and re.match(
# First character of the domain
rf"^(?:[a-z0-9{r'_?'if rfc_2782 else ''}]"
rf"^(?:[a-z0-9{service_record}]"
# Sub-domain
+ rf"(?:[a-z0-9-{r'_?'if rfc_2782 else ''}]{{0,61}}"
+ rf"(?:[a-z0-9-{service_record}]{{0,61}}"
# Hostname
+ rf"[a-z0-9{r'_?'if rfc_2782 else ''}])?\.)"
+ 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]{r'.?$' if rfc_1034 else r'$'}",
+ rf"[a-z]{trailing_dot}",
value.encode("idna").decode("utf-8"),
re.IGNORECASE,
)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
("3.cn.", True, False),
("_example.com", False, True),
("example_.com", False, True),
("_exa_mple_.com", False, True),
("a.cn", False, False),
("sub1.sub2.sample.co.uk", False, False),
("somerandomexample.xn--fiqs8s", False, False),
Expand Down Expand Up @@ -67,6 +68,9 @@ def test_returns_true_on_valid_top_level_domain(
("_example._com", False, False),
("example_.com", False, False),
("example", False, False),
("example.com!", True, False),
("example?.com", True, False),
("__exa__mple__.com", False, True),
("a......b.com", False, False),
("a.123", False, False),
("123.123", False, False),
Expand Down

0 comments on commit 181f6dc

Please sign in to comment.