Repository navigation
Expand file tree
/
Copy pathvalidators.py
More file actions
64 lines (51 loc) · 2.07 KB
/
Copy pathvalidators.py
File metadata and controls
64 lines (51 loc) · 2.07 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Any, Dict, List, Tuple, Type
class ValidationError(Exception):
"""Raised when input data fails validation rules."""
pass
class RecordValidator:
"""Validates dictionary records against a defined structural schema."""
def __init__(self, schema: Dict[str, Tuple[Type, bool]]):
# Schema format: {'field_name': (expected_type, is_required)}
self.schema = schema
def validate_record(self, record: Dict[str, Any]) -> Tuple[bool, List[str]]:
"""Check a single record against schema rules and collect errors."""
if not isinstance(record, dict):
return False, ["Input record must be a dictionary"]
errors = []
for field, (expected_type, required) in self.schema.items():
if field not in record or record[field] is None:
if required:
errors.append(f"Missing required field: '{field}'")
continue
val = record[field]
if not isinstance(val, expected_type):
actual_type = type(val).__name__
errors.append(
f"Field '{field}' expected {expected_type.__name__}, got {actual_type}"
)
return len(errors) == 0, errors
def validate_processing_loop(
dataset: List[Dict[str, Any]],
validator: RecordValidator,
stop_on_first_error: bool = False
) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""
Runs validation across a stream or sequence of input items.
Returns tuples of (valid_records, failed_records_with_details).
"""
valid_records = []
failed_records = []
for index, item in enumerate(dataset):
is_valid, errors = validator.validate_record(item)
if is_valid:
valid_records.append(item)
else:
failure_info = {
"index": index,
"item": item,
"errors": errors
}
failed_records.append(failure_info)
if stop_on_first_error:
break
return valid_records, failed_records