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)
| 11 | def 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 | |
| 52 | def validate_api_key(api_key: str, min_length: int = 10) -> str: |
| 53 | """ |
| 54 | Validate API key format. |
| 55 |
no test coverage detected