Skip to content

Commit 9b8d48c

Browse files
committed
feat: allow custom URL scheme validation
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, ...)
1 parent c9585e9 commit 9b8d48c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/validators/url.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# standard
44
from functools import lru_cache
55
import re
6-
from typing import Optional
6+
from typing import Callable, Optional
77
from urllib.parse import parse_qs, unquote, urlsplit
88

99
# local
@@ -165,6 +165,7 @@ def url(
165165
private: Optional[bool] = None, # only for ip-addresses
166166
rfc_1034: bool = False,
167167
rfc_2782: bool = False,
168+
validate_scheme: Callable[[str], bool] = _validate_scheme,
168169
):
169170
r"""Return whether or not given value is a valid URL.
170171
@@ -213,6 +214,8 @@ def url(
213214
rfc_2782:
214215
Domain/Host name is of type service record.
215216
Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).
217+
validate_scheme:
218+
Function that validates URL scheme.
216219
217220
Returns:
218221
(Literal[True]): If `value` is a valid url.
@@ -229,7 +232,7 @@ def url(
229232
return False
230233

231234
return (
232-
_validate_scheme(scheme)
235+
validate_scheme(scheme)
233236
and _validate_netloc(
234237
netloc,
235238
skip_ipv6_addr,

0 commit comments

Comments
 (0)