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
28 changes: 18 additions & 10 deletions src/validators/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _validate_scheme(value: str):
value
# fmt: off
in {
"file",
"ftp",
"ftps",
"git",
Expand Down Expand Up @@ -240,18 +241,25 @@ def url(
except ValueError:
return False

# RFC 8089: a "file" URL's authority is commonly empty or "localhost",
# neither of which is a hostname `_validate_netloc` would accept.
is_local_file = scheme == "file" and netloc in ("", "localhost")

return (
validate_scheme(scheme)
and _validate_netloc(
netloc,
skip_ipv6_addr,
skip_ipv4_addr,
may_have_port,
simple_host,
consider_tld,
private,
rfc_1034,
rfc_2782,
and (
is_local_file
or _validate_netloc(
netloc,
skip_ipv6_addr,
skip_ipv4_addr,
may_have_port,
simple_host,
consider_tld,
private,
rfc_1034,
rfc_2782,
)
)
and _validate_optionals(path, query, fragment, strict_query)
)
13 changes: 13 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,16 @@ def test_returns_failed_validation_on_invalid_url(value: str):
def test_returns_failed_validation_on_invalid_private_url(value: str, private: Optional[bool]):
"""Test returns failed validation on invalid private url."""
assert isinstance(url(value, private=private), ValidationError)


@pytest.mark.parametrize(
"value",
[
"file:///var/www",
"file:///C:/Windows",
"file://localhost/var/www",
],
)
def test_returns_string_for_valid_file_url(value: str):
"""Test returns true on valid file url. Ref: RFC 8089."""
assert url(value)