validators
¶
Validation utilities for Music Genre Updater.
SupportsDictConversion
¶
SecurityValidationError
¶
Bases: Exception
Exception raised when security validation fails.
Initialize the security validation error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message describing the validation failure |
required |
field
|
str | None
|
The field that failed validation |
None
|
dangerous_pattern
|
str | None
|
The specific pattern that triggered the error |
None
|
Source code in src/core/models/validators.py
SecurityValidator
¶
Comprehensive security validator for input sanitization and validation.
This class provides methods to validate and sanitize various types of input to prevent security vulnerabilities including injection attacks, path traversal, and malicious content.
Initialize the security validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Optional logger instance for security event logging |
None
|
Source code in src/core/models/validators.py
validate_track_data
¶
Validate and sanitize track data dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_data
|
dict[str, Any]
|
Track data dictionary to validate |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Validated and sanitized track data |
Raises:
| Type | Description |
|---|---|
SecurityValidationError
|
If validation fails |
Source code in src/core/models/validators.py
sanitize_string
staticmethod
¶
Sanitize a string value by removing dangerous characters and patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string value to sanitize |
required |
field_name
|
str | None
|
Optional field name for logging |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The sanitized string |
Raises:
| Type | Description |
|---|---|
SecurityValidationError
|
If validation fails |
Source code in src/core/models/validators.py
validate_file_path
¶
Validate a file path to prevent path traversal attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The file path to validate |
required |
allowed_base_paths
|
list[str] | None
|
Optional list of allowed base paths |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The validated and normalized file path |
Raises:
| Type | Description |
|---|---|
SecurityValidationError
|
If validation fails |
Source code in src/core/models/validators.py
validate_api_input
¶
Validate and sanitize API input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Input data dictionary to validate |
required |
max_depth
|
int
|
Maximum recursion depth allowed (default: 10) |
10
|
_current_depth
|
int
|
Internal parameter for tracking recursion depth |
0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Validated and sanitized data |
Raises:
| Type | Description |
|---|---|
SecurityValidationError
|
If validation fails or max depth exceeded |
Source code in src/core/models/validators.py
is_valid_year
¶
Check if the given value is a valid 4-digit year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year_str
|
str | float | None
|
Value to validate as a year |
required |
min_year
|
int | None
|
Minimum valid year (default: 1900) |
None
|
current_year
|
int | None
|
Current year for upper bound (default: current year) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if valid year, False otherwise. |
Source code in src/core/models/validators.py
is_empty_year
¶
Check if a year value is considered empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year_value
|
Any
|
Year value to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the year is empty (None, empty string, or whitespace-only) |
Source code in src/core/models/validators.py
is_valid_track_item
¶
Validate that the given object is a track dictionary.
This performs runtime type checking to ensure the object has the required structure of a TrackDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
Object to validate |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[TrackDict]
|
True if the object is a valid TrackDict, False otherwise |
Source code in src/core/models/validators.py
validate_track_ids
¶
Return list of numeric track IDs that are not equal to the year value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_ids
|
list[str]
|
List of track IDs to validate |
required |
year
|
str
|
Year value to check against |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of valid track IDs |
Source code in src/core/models/validators.py
validate_artist_name
¶
Validate artist name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str | None
|
Artist name to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise |
Source code in src/core/models/validators.py
validate_album_name
¶
Validate album name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
album
|
str | None
|
Album name to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise |