One function

domain in python-validators/validators

The author described this change as fix: rfc cases in the `domain` validator. It counts as a record because the check below fails on the code as it stood at f04a8c53e and passes on 181f6dcdb, with nothing else changed between the two runs.

Fix saved2024-04-18
Sharing licenceMIT · LICENSE.txt
Change size+9 5

What the code was meant to do, written into the code itself as a docstring

Return whether or not given value is a valid domain. Examples: Args: value: Domain string to validate. consider_tld: Restrict domain to TLDs allowed by IANA. rfc_1034: Allows optional trailing dot in the domain name. Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034). rfc_2782: Domain name is of type service record. Allows optional underscores in the domain name. Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782). Returns: (Literal[True]): If `value` is a valid domain name. (ValidationError): If `value` is an invalid domain name. Raises: (UnicodeError): If `value` cannot be encoded into `idna` or decoded into `utf-8`.

The change

4040 return False
4141
4242 try:
43- return not re.search(r"\s", value) and re.match(
43+
44+ service_record = r"_" if rfc_2782 else ""
45+ trailing_dot = r"\.?$" if rfc_1034 else r"$"
46+
47+ return not re.search(r"\s|__+", value) and re.match(
4448 # First character of the domain
45- rf"^(?:[a-z0-9{r'_?'if rfc_2782 else ''}]"
49+ rf"^(?:[a-z0-9{service_record}]"
4650 # Sub-domain
47- + rf"(?:[a-z0-9-{r'_?'if rfc_2782 else ''}]{{0,61}}"
51+ + rf"(?:[a-z0-9-{service_record}]{{0,61}}"
4852 # Hostname
49- + rf"[a-z0-9{r'_?'if rfc_2782 else ''}])?\.)"
53+ + rf"[a-z0-9{service_record}])?\.)"
5054 # First 61 characters of the gTLD
5155 + r"+[a-z0-9][a-z0-9-_]{0,61}"
5256 # Last character of the gTLD
53- + rf"[a-z]{r'.?$' if rfc_1034 else r'$'}",
57+ + rf"[a-z]{trailing_dot}",
5458 value.encode("idna").decode("utf-8"),
5559 re.IGNORECASE,
5660 )

The check that tells the two apart

failpass·tests/test_domain.py::test_returns_failed_validation_on_invalid_domain[example.com!-True-False]

Check file tests/test_domain.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before itf04a8c53ed3b6c2a6d678ead785015730409e4e9
Broken version dated2024-04-12
Modulevalidators.domain
Units changeddomain
Fingerprint6e79f4d27c7b3112
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in python-validators/validators