fileaudit.csv_check module#
License MPL-2.0 (C) 2026 Created by Maikel Mardjan - https://nocomplexity.com/ FileAudit - CSV File Security Checker
- exception fileaudit.csv_check.CsvValidationError(message)[source]#
Bases:
ExceptionCustom exception for CSV validation failures in FileAudit.
- fileaudit.csv_check.validate_csv(func_or_path=None, max_file_size=None, max_rows=None, max_columns=None, max_field_size=None, max_total_fields=None, max_row_size=None, max_filename_length=None, reject_formula_injection=True, reject_control_characters=True, encoding='utf-8', dialect='excel')[source]#
Validate a CSV file directly or validate CSV arguments with a decorator.
This function supports both direct CSV validation and decorator usage.
In direct-invocation mode, the CSV file is validated immediately and the function returns
Truewhen validation succeeds orFalsewhen validation fails. Local file paths,Pathobjects, and URLs are supported.In decorator mode, the decorated function is called only after its CSV argument has successfully passed validation. A validation failure raises
CsvValidationError.func_or_pathis interpreted based on its value. A callable is treated as the function to decorate. APathor a string that looks like a file path or URL is treated as a CSV source for direct validation. A string that does not look like a path or URL can be used to explicitly identify the decorated function argument containing the CSV path.Examples
Validate a local CSV file:
validate_csv("data.csv")
Validate a CSV file from a URL:
validate_csv("https://example.com/data.csv")
Use as a decorator, using the first function argument as the CSV path:
@validate_csv def process_csv(csv_path): ...
Use as a decorator with default validation options:
@validate_csv() def process_csv(csv_path): ...
Specify the decorated function argument containing the CSV path:
@validate_csv("input_file") def process_csv(input_file): ...
Configure validation limits:
@validate_csv( max_file_size=10 * 1024 * 1024, max_rows=10_000, max_columns=50, ) def process_csv(csv_path): ...
The explicit argument name must match a parameter of the decorated function. Otherwise, the decorator uses the function’s first parameter as the CSV path:
@validate_csv("csv_path") def process_csv(csv_path): ...
- Parameters:
func_or_path – Controls whether the function operates in direct or decorator mode. A callable is treated as the function to decorate. A
Pathor a string that looks like a file path or URL is treated as a CSV source for direct validation. A string that does not look like a file path or URL is treated as the name of the decorated function argument containing the CSV path. If that name does not match a function parameter, the first function parameter is used instead.Nonecreates a decorator that uses the first function parameter as the CSV path.max_file_size – Maximum allowed CSV file size in bytes. If
None, uses the default maximum file size.max_rows – Maximum number of rows allowed in the CSV file. If
None, uses the default maximum number of rows.max_columns – Maximum number of columns allowed in the CSV file. If
None, uses the default maximum number of columns.max_field_size – Maximum allowed size of an individual CSV field. If
None, uses the default maximum field size.max_total_fields – Maximum total number of fields allowed in the CSV file. If
None, uses the default maximum.max_row_size – Maximum allowed size of an individual CSV row. If
None, uses the default maximum row size.max_filename_length – Maximum allowed length of the CSV filename. If
None, uses the default maximum filename length.reject_formula_injection – Whether to reject fields that could be interpreted as spreadsheet formulas. Defaults to
True.reject_control_characters – Whether to reject disallowed control characters in CSV fields. Defaults to
True.encoding – Character encoding used to read the CSV file. Defaults to
"utf-8".dialect – CSV dialect used when parsing the file. Defaults to
"excel".
- Returns:
In direct-invocation mode,
Trueif the CSV passes validation orFalseif validation fails.In decorator mode, the decorated function’s wrapped callable is returned. When invoked, the wrapped function returns the original function’s return value after successful CSV validation.
- Raises:
CsvValidationError – If
func_or_pathis not a callable,Path, string, orNone.CsvValidationError – If a decorated function has no parameters.
CsvValidationError – If the decorated function is called with an invalid argument signature.
CsvValidationError – If the CSV path argument is missing or is neither a string nor a
Path.CsvValidationError – If CSV validation fails while the function is used as a decorator.