Skip to content

Commit

Permalink
feat: allow custom URL scheme validation
Browse files Browse the repository at this point in the history
Enhances `validators.url` to allow
- restricting the allowed schemes (e.g. to accept only https,
                                   and nothing else)
- relaxing the allowed schemes to also accept less known schemes
  (e.g. ws, wss, ldap, ...)
  • Loading branch information
e3krisztian committed Oct 29, 2024
1 parent c9585e9 commit 9b8d48c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/validators/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# standard
from functools import lru_cache
import re
from typing import Optional
from typing import Callable, Optional
from urllib.parse import parse_qs, unquote, urlsplit

# local
Expand Down Expand Up @@ -165,6 +165,7 @@ def url(
private: Optional[bool] = None, # only for ip-addresses
rfc_1034: bool = False,
rfc_2782: bool = False,
validate_scheme: Callable[[str], bool] = _validate_scheme,
):
r"""Return whether or not given value is a valid URL.
Expand Down Expand Up @@ -213,6 +214,8 @@ def url(
rfc_2782:
Domain/Host name is of type service record.
Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).
validate_scheme:
Function that validates URL scheme.
Returns:
(Literal[True]): If `value` is a valid url.
Expand All @@ -229,7 +232,7 @@ def url(
return False

return (
_validate_scheme(scheme)
validate_scheme(scheme)
and _validate_netloc(
netloc,
skip_ipv6_addr,
Expand Down

0 comments on commit 9b8d48c

Please sign in to comment.