Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit faed951

Browse filesBrowse files
authored
Merge pull request #367 from yozachar/workshop
fix: rfc cases in the `domain` validator
2 parents f04a8c5 + 181f6dc commit faed951
Copy full SHA for faed951

File tree

Expand file treeCollapse file tree

2 files changed

+13
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-5
lines changed

‎src/validators/domain.py

Copy file name to clipboardExpand all lines: src/validators/domain.py
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,21 @@ def domain(
5959
return False
6060

6161
try:
62-
return not re.search(r"\s", value) and re.match(
62+
63+
service_record = r"_" if rfc_2782 else ""
64+
trailing_dot = r"\.?$" if rfc_1034 else r"$"
65+
66+
return not re.search(r"\s|__+", value) and re.match(
6367
# First character of the domain
64-
rf"^(?:[a-z0-9{r'_?'if rfc_2782 else ''}]"
68+
rf"^(?:[a-z0-9{service_record}]"
6569
# Sub-domain
66-
+ rf"(?:[a-z0-9-{r'_?'if rfc_2782 else ''}]{{0,61}}"
70+
+ rf"(?:[a-z0-9-{service_record}]{{0,61}}"
6771
# Hostname
68-
+ rf"[a-z0-9{r'_?'if rfc_2782 else ''}])?\.)"
72+
+ rf"[a-z0-9{service_record}])?\.)"
6973
# First 61 characters of the gTLD
7074
+ r"+[a-z0-9][a-z0-9-_]{0,61}"
7175
# Last character of the gTLD
72-
+ rf"[a-z]{r'.?$' if rfc_1034 else r'$'}",
76+
+ rf"[a-z]{trailing_dot}",
7377
value.encode("idna").decode("utf-8"),
7478
re.IGNORECASE,
7579
)

‎tests/test_domain.py

Copy file name to clipboardExpand all lines: tests/test_domain.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
("3.cn.", True, False),
2121
("_example.com", False, True),
2222
("example_.com", False, True),
23+
("_exa_mple_.com", False, True),
2324
("a.cn", False, False),
2425
("sub1.sub2.sample.co.uk", False, False),
2526
("somerandomexample.xn--fiqs8s", False, False),
@@ -67,6 +68,9 @@ def test_returns_true_on_valid_top_level_domain(
6768
("_example._com", False, False),
6869
("example_.com", False, False),
6970
("example", False, False),
71+
("example.com!", True, False),
72+
("example?.com", True, False),
73+
("__exa__mple__.com", False, True),
7074
("a......b.com", False, False),
7175
("a.123", False, False),
7276
("123.123", False, False),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.