Add validation text improvements
Text Validation Implementation (Issue #227)
Summary
Implemented missing payload range checks and empty string validation in src/utils/text.py to catch edge cases early and prevent resource wastage.
Implementation Details
Files Modified
-
src/utils/text.py
-
Added
loggingimport and logger setup -
Added
ValidationErrorimport fromsrc.utils.exceptions -
Added
MAX_STRING_LENGTH = 10 * 1024 * 1024constant (10 MB limit) -
Added
validate_input_text()utility function that checks:- None input → raises
ValidationError - Non-string types → raises
TypeError - Empty/whitespace-only strings → raises
ValidationError - Oversized payloads (>10MB) → logs warning and raises
ValidationError
- None input → raises
-
Updated 5 parsing functions with validation guard clauses:
parse_raw_response_to_classifications()parse_raw_response_to_profiles()parse_raw_response_to_events()parse_raw_response_to_event()parse_raw_response_to_image()
-
-
tests/unit/test_utils.py
- Added 3 test classes with 30+ test cases:
TestTextValidationEmptyAndNull: Tests empty/null/whitespace rejection (15 tests)TestTextValidationTypeChecks: Tests type checking (4 tests)TestTextValidationLengthConstraints: Tests max length enforcement (4 tests)TestTextValidationRegressions: Tests valid inputs still work (8+ tests)
- Added 3 test classes with 30+ test cases:
Validation Rules
- Empty Check: Rejects empty strings and whitespace-only input
- Type Check: Rejects non-string inputs with clear error message
- Length Check: Rejects strings > 10MB with logging warning
- Early Exit: All validation happens at function entry points
- Clear Errors: Each check provides descriptive error messages
Testing Strategy
- Comprehensive coverage of edge cases: None, empty, whitespace, oversized
- Type validation for each parsing function
- Regression tests ensuring valid inputs still parse correctly
- NO_EVENT keyword handling preserved
- All 30+ tests grouped into logical test classes
Key Design Decisions
- 10MB threshold: Reasonable for text processing pipelines (not too restrictive)
- Logging on overflow: Warns about suspicious large inputs before failing
- Guard clauses at entry: Prevents downstream processing of invalid data
- Consistent error handling: Uses existing
ValidationErrorandTypeError - No performance regression: Validation is lightweight and early-exit friendly
This PR adds input validation to the five Safe to merge with minor fixes; validation logic is correct and well-tested. The The len()-vs-bytes labelling in src/utils/text.py and the double-validation path in parse_raw_response_to_event are the two spots worth a second look.Greptile Summary
parse_raw_response_to_* functions in src/utils/text.py, catching None, empty/whitespace-only, wrong-type, and oversized inputs early via a new validate_input_text helper, backed by 30+ unit tests.
validate_input_text raises ValidationError for None/empty and TypeError for non-strings; a configurable MAX_STRING_LENGTH (10 \u00d7 1024 \u00d7 1024) guards against oversized payloads.validate_input_text as a guard clause at entry, with updated docstrings listing the new exceptions.NO_EVENT sentinel path.Confidence Score: 4/5
len() check counts Unicode characters, not UTF-8 bytes, so the '10 MB' comment and 'bytes' labels in the log and error message are inaccurate for multi-byte Unicode input. Additionally, parse_raw_response_to_event runs validate_input_text twice by calling the helper then delegating to parse_raw_response_to_events. Neither issue causes incorrect rejection or acceptance of input.Important Files Changed
Filename
Overview
src/utils/text.py
Adds validate_input_text helper and guards all five parse functions; len() counts characters not bytes (mislabelled as bytes in constant comment, log, and error), and parse_raw_response_to_event validates twice.
tests/unit/test_utils.py
Adds 30+ tests across four well-structured classes; parse_raw_response_to_event is missing from the length-constraint and type-check test classes.
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Caller] --> B["parse_raw_response_to_*(content)"]
B --> C["validate_input_text(content, fn_name)"]
C --> D{content is None?}
D -->|Yes| E[raise ValidationError: cannot be None]
D -->|No| F{"isinstance(content, str)?"}
F -->|No| G[raise TypeError: must be string]
F -->|Yes| H{"content.strip() falsy?"}
H -->|Yes| I[raise ValidationError: cannot be empty]
H -->|No| J{"len > MAX_STRING_LENGTH?"}
J -->|Yes| K[log warning + raise ValidationError]
J -->|No| L[validation passed]
L --> M[parse function body runs]
M --> N[Return structured result]
Reviews (1): Last reviewed commit: "Add validation text improvements" | Re-trigger Greptile