Coverage for src / ezcompiler / utils / validators / string_validators.py: 42.31%
18 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 06:49 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 06:49 +0000
1# ///////////////////////////////////////////////////////////////
2# STRING_VALIDATORS - String validation utilities
3# Project: ezcompiler
4# ///////////////////////////////////////////////////////////////
6"""
7String validators - Validation utilities for string manipulation and validation.
9This module provides validation functions for sanitizing filenames and
10validating string patterns.
11"""
13from __future__ import annotations
15# ///////////////////////////////////////////////////////////////
16# IMPORTS
17# ///////////////////////////////////////////////////////////////
18# Standard library imports
19import re
21# Local imports
22from ...shared.exceptions.utils.validation_exceptions import PatternValidationError
24# ///////////////////////////////////////////////////////////////
25# FUNCTIONS
26# ///////////////////////////////////////////////////////////////
29def sanitize_filename(filename: str) -> str:
30 """
31 Sanitize a filename by removing invalid characters.
33 Args:
34 filename: Original filename
36 Returns:
37 str: Sanitized filename with invalid characters removed
39 Note:
40 Returns "unnamed_file" if filename becomes empty after sanitization.
42 Example:
43 >>> sanitize_filename("my_file.txt")
44 'my_file.txt'
45 >>> sanitize_filename("invalid<>file.txt")
46 'invalid__file.txt'
47 >>> sanitize_filename(">>>")
48 'unnamed_file'
49 """
50 if not isinstance(filename, str):
51 return ""
53 # Remove or replace invalid characters
54 invalid_chars = r'[<>:"/\\|?*]'
55 sanitized = re.sub(invalid_chars, "_", filename)
57 # Remove leading/trailing spaces and dots
58 sanitized = sanitized.strip(" .")
60 # Ensure filename is not empty
61 if not sanitized:
62 sanitized = "unnamed_file"
64 return sanitized
67def validate_pattern(
68 value: str,
69 pattern: str,
70 field_name: str = "Value",
71 error_msg: str | None = None,
72) -> None:
73 r"""
74 Validate that a string matches a regex pattern.
76 Args:
77 value: String to validate
78 pattern: Regex pattern to match
79 field_name: Name of field for error messages
80 error_msg: Custom error message
82 Raises:
83 TypeError: If value is not a string
84 PatternValidationError: If value doesn't match pattern
86 Example:
87 >>> validate_pattern("hello123", r"^[a-z]+\d+$")
88 >>> validate_pattern("hello", r"^\d+$")
89 Traceback (most recent call last):
90 ...
91 PatternValidationError: Value does not match required pattern
92 """
93 if not isinstance(value, str): 93 ↛ 94line 93 didn't jump to line 94 because the condition on line 93 was never true
94 raise TypeError(f"{field_name} must be a string")
96 if not re.match(pattern, value): 96 ↛ exitline 96 didn't return from function 'validate_pattern' because the condition on line 96 was always true
97 msg = error_msg or f"{field_name} does not match required pattern"
98 raise PatternValidationError(msg)