MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / validate_url

Function validate_url

codewiki/cli/utils/validation.py:13–52  ·  view source on GitHub ↗

Validate URL format. Args: url: URL to validate require_https: Require HTTPS scheme (except localhost) allow_localhost: Allow localhost URLs Returns: Validated URL Raises: ConfigurationError: If URL is invalid

(url: str, require_https: bool = False, allow_localhost: bool = True)

Source from the content-addressed store, hash-verified

11def validate_url(url: str, require_https: bool = False, allow_localhost: bool = True) -> str:
12 """
13 Validate URL format.
14
15 Args:
16 url: URL to validate
17 require_https: Require HTTPS scheme (except localhost)
18 allow_localhost: Allow localhost URLs
19
20 Returns:
21 Validated URL
22
23 Raises:
24 ConfigurationError: If URL is invalid
25 """
26 try:
27 parsed = urlparse(url)
28
29 # Check scheme
30 if not parsed.scheme:
31 raise ConfigurationError(f"Invalid URL (missing scheme): {url}")
32
33 # Check HTTPS requirement
34 if require_https and parsed.scheme != "https":
35 # Allow HTTP for localhost
36 if allow_localhost and parsed.hostname in ["localhost", "127.0.0.1", "::1"]:
37 pass
38 else:
39 raise ConfigurationError(
40 f"URL must use HTTPS: {url}\nHTTP is only allowed for localhost"
41 )
42
43 # Check hostname
44 if not parsed.hostname:
45 raise ConfigurationError(f"Invalid URL (missing hostname): {url}")
46
47 return url
48 except ValueError as e:
49 raise ConfigurationError(f"Invalid URL format: {url}\nError: {e}")
50
51
52def validate_api_key(api_key: str, min_length: int = 10) -> str:
53 """
54 Validate API key format.
55

Callers 3

config_setFunction · 0.90
config_validateFunction · 0.90
validateMethod · 0.90

Calls 1

ConfigurationErrorClass · 0.90

Tested by

no test coverage detected