Skip to content

Commit 9dac863

Browse files
authored
feat: allow custom URL scheme validation (#409)
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 894a069 commit 9dac863

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/validators/url.py

+5-2
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
@@ -174,6 +174,7 @@ def url(
174174
private: Optional[bool] = None, # only for ip-addresses
175175
rfc_1034: bool = False,
176176
rfc_2782: bool = False,
177+
validate_scheme: Callable[[str], bool] = _validate_scheme,
177178
):
178179
r"""Return whether or not given value is a valid URL.
179180
@@ -222,6 +223,8 @@ def url(
222223
rfc_2782:
223224
Domain/Host name is of type service record.
224225
Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).
226+
validate_scheme:
227+
Function that validates URL scheme.
225228
226229
Returns:
227230
(Literal[True]): If `value` is a valid url.
@@ -238,7 +241,7 @@ def url(
238241
return False
239242

240243
return (
241-
_validate_scheme(scheme)
244+
validate_scheme(scheme)
242245
and _validate_netloc(
243246
netloc,
244247
skip_ipv6_addr,

0 commit comments

Comments
 (0)