Skip to content

Commit bbf4600

Browse files
authored
Merge pull request #244 from joe733/workshop
maint: improve `domain`, `email` & `hostname`
2 parents 050a2c2 + 060c96d commit bbf4600

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

validators/domain.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):
4545
> *New in version 0.9.0*.
4646
"""
4747
try:
48-
return not re.search(r"\s", value) and re.compile(
48+
return not re.search(r"\s", value) and re.match(
4949
# First character of the domain
5050
rf"^(?:[a-zA-Z0-9{'_'if rfc_2782 else ''}]"
5151
# Sub domain + hostname
5252
+ r"(?:[a-zA-Z0-9-_]{0,61}[A-Za-z0-9])?\.)"
5353
# First 61 characters of the gTLD
5454
+ r"+[A-Za-z0-9][A-Za-z0-9-_]{0,61}"
5555
# Last character of the gTLD
56-
+ rf"[A-Za-z]{r'.$' if rfc_1034 else r'$'}"
57-
).match(value.encode("idna").decode("ascii"))
56+
+ rf"[A-Za-z]{r'.$' if rfc_1034 else r'$'}",
57+
value.encode("idna").decode("utf-8"),
58+
re.IGNORECASE,
59+
)
5860
except UnicodeError:
5961
return False

validators/email.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ def email(
1414
value: str,
1515
/,
1616
*,
17+
ipv6_address: bool = False,
18+
ipv4_address: bool = False,
1719
simple_host: bool = False,
18-
ip_address: bool = False,
1920
rfc_1034: bool = False,
2021
rfc_2782: bool = False,
2122
):
@@ -38,10 +39,12 @@ def email(
3839
Args:
3940
value:
4041
eMail string to validate.
42+
ipv6_address:
43+
When the domain part is an IPv6 address.
44+
ipv4_address:
45+
When the domain part is an IPv4 address.
4146
simple_host:
4247
When the domain part is a simple hostname.
43-
ip_address:
44-
When the domain part is an IP address.
4548
rfc_1034:
4649
Allow trailing dot in domain name.
4750
Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034).
@@ -66,7 +69,7 @@ def email(
6669
# ref: RFC 1034 and 5231
6770
return False
6871

69-
if ip_address:
72+
if ipv6_address or ipv4_address:
7073
if domain_part.startswith("[") and domain_part.endswith("]"):
7174
# ref: RFC 5321
7275
domain_part = domain_part.lstrip("[").rstrip("]")
@@ -77,7 +80,8 @@ def email(
7780
bool(
7881
hostname(
7982
domain_part,
80-
skip_ip_addr=not ip_address,
83+
skip_ipv6_addr=not ipv6_address,
84+
skip_ipv4_addr=not ipv4_address,
8185
may_have_port=False,
8286
maybe_simple=simple_host,
8387
rfc_1034=rfc_1034,

validators/hostname.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ def hostname(
4646
value: str,
4747
/,
4848
*,
49+
skip_ipv6_addr: bool = False,
50+
skip_ipv4_addr: bool = False,
4951
may_have_port: bool = True,
50-
skip_ip_addr: bool = False,
5152
maybe_simple: bool = True,
5253
rfc_1034: bool = False,
5354
rfc_2782: bool = False,
@@ -79,10 +80,12 @@ def hostname(
7980
Args:
8081
value:
8182
Hostname string to validate.
83+
skip_ipv6_addr:
84+
When hostname string cannot be an IPv6 address.
85+
skip_ipv4_addr:
86+
When hostname string cannot be an IPv4 address.
8287
may_have_port:
8388
Hostname string may contain port number.
84-
skip_ip_addr:
85-
When hostname string cannot be an IP address.
8689
maybe_simple:
8790
Hostname string maybe only hyphens and alpha-numerals.
8891
rfc_1034:
@@ -104,13 +107,13 @@ def hostname(
104107
return (
105108
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)
106109
or domain(host_seg, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
107-
or (False if skip_ip_addr else ipv4(host_seg, cidr=False))
108-
or (False if skip_ip_addr else ipv6(host_seg, cidr=False))
110+
or (False if skip_ipv4_addr else ipv4(host_seg, cidr=False))
111+
or (False if skip_ipv6_addr else ipv6(host_seg, cidr=False))
109112
)
110113

111114
return (
112115
(_simple_hostname_regex().match(value) if maybe_simple else False)
113116
or domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
114-
or (False if skip_ip_addr else ipv4(value, cidr=False))
115-
or (False if skip_ip_addr else ipv6(value, cidr=False))
117+
or (False if skip_ipv4_addr else ipv4(value, cidr=False))
118+
or (False if skip_ipv6_addr else ipv6(value, cidr=False))
116119
)

0 commit comments

Comments
 (0)