Skip to content

Commit 4d18d55

Browse files
committed
fix: validator
1 parent 583a2b6 commit 4d18d55

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import asyncio
2-
from typing import Callable
2+
from typing import Callable, Any
33

44

5-
def is_async_function(func: Callable) -> bool:
6-
"""
7-
check if the function is an async function
8-
>>> is_async_function(lambda x: x)
9-
False
5+
def is_async_function(func: Callable[..., Any]) -> bool:
106
"""
7+
Check if the function is an async function.
8+
9+
Args:
10+
func (Callable[..., Any]): The function to check.
1111
12+
Returns:
13+
bool: True if the function is async, False otherwise.
14+
15+
Examples:
16+
>>> async def async_func(): pass
17+
>>> is_async_function(async_func)
18+
True
19+
>>> is_async_function(lambda x: x)
20+
False
21+
"""
1222
return asyncio.iscoroutinefunction(func)

src/usepy/validator/is_url.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1-
def is_url(url: str) -> bool:
1+
from urllib.parse import urlparse
2+
from typing import List, Optional
3+
import logging
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
def is_url(url: str, allowed_schemes: Optional[List[str]] = None) -> bool:
29
"""
3-
check if the given string is a valid URL.
10+
Check if the given string is a valid URL.
411
512
Args:
6-
url (str): the string to check.
13+
url (str): The string to check.
14+
allowed_schemes (Optional[List[str]]): List of allowed URL schemes.
15+
Defaults to ["http", "https"] if None.
716
817
Returns:
9-
bool: if the string is a valid URL, return True, otherwise return False.
18+
bool: If the string is a valid URL, return True, otherwise return False.
1019
1120
Examples:
1221
>>> is_url("https://www.google.com")
1322
True
23+
>>> is_url("ftp://example.com", allowed_schemes=["ftp"])
24+
True
25+
>>> is_url("mailto:user@example.com", allowed_schemes=["mailto"])
26+
True
1427
"""
1528
if not url or not isinstance(url, str):
1629
return False
1730

18-
from urllib.parse import urlparse
31+
if allowed_schemes is None:
32+
allowed_schemes = ["http", "https"]
1933

2034
try:
2135
result = urlparse(url)
22-
if all([result.scheme, result.netloc]) and result.scheme in [
23-
"http",
24-
"https",
25-
]:
36+
if result.scheme and result.scheme in allowed_schemes:
2637
return True
2738
except ValueError:
39+
logger.debug(f"Invalid URL format: {url}")
2840
return False
2941

3042
return False

tests/test_validator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ def test_is_url():
1717
assert is_url("http://localhost:8000")
1818
assert not is_url("not a url")
1919
assert not is_url("mailto:user@example.com")
20+
assert is_url("mailto:user@example.com", allowed_schemes=["mailto"])
2021
assert not is_url("ftp://invalid-scheme.com")

0 commit comments

Comments
 (0)