-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
37 lines (31 loc) · 1.31 KB
/
Copy pathvalidators.py
File metadata and controls
37 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import logging
from typing import Any, Optional
logger = logging.getLogger(__name__)
def validate_input(data: Any, expected_type: type) -> Optional[Any]:
"""Ensures input matches expected type with edge case handling."""
try:
if data is None:
raise ValueError("input data cannot be null")
if not isinstance(data, expected_type):
raise TypeError(f"expected {expected_type.__name__}, got {type(data).__name__}")
return data
except (ValueError, TypeError) as e:
logger.error(f"validation failure: {e}")
return None
def safe_int_conversion(value: Any, default: int = 0) -> int:
"""Converts values to int with fallback for errors."""
try:
if isinstance(value, (int, float)):
return int(value)
if isinstance(value, str):
return int(value.strip())
raise ValueError("unsupported type for conversion")
except (ValueError, TypeError, AttributeError) as e:
logger.warning(f"conversion failed for {value}: {e}. returning default.")
return default
def validate_non_empty_string(value: Any) -> str:
"""Checks if string is not empty or whitespace only."""
if not isinstance(value, str):
return ""
stripped = value.strip()
return stripped if stripped else ""