| import re |
| from typing import Optional |
| from .config import VALIDATOR_ENABLED |
|
|
| class ValidationResult: |
| def __init__(self, is_valid: bool, reason: str = "", score: float = 1.0): |
| self.is_valid = is_valid |
| self.reason = reason |
| self.score = score |
|
|
| class DataValidator: |
| GARBAGE_PATTERNS = [ |
| re.compile(r'^[\s\[\]\(\)\{\}<>\.\,\?\!\"\'\;\:\-\_\@\#\$\%\^\&\*\+\=\\\/\|~`]{1,10}$'), |
| ] |
|
|
| def validate_input(self, text: str) -> ValidationResult: |
| if not VALIDATOR_ENABLED: |
| return ValidationResult(True, "validator disabled", 1.0) |
| if not text or not text.strip(): |
| return ValidationResult(False, "empty input", 0.0) |
| if len(text.strip()) < 2: |
| return ValidationResult(False, "input too short", 0.1) |
| for pat in self.GARBAGE_PATTERNS: |
| if pat.match(text.strip()): |
| return ValidationResult(False, "non-semantic input", 0.0) |
| return ValidationResult(True, "", 1.0) |
|
|
| def validate_output(self, text: str) -> ValidationResult: |
| if not VALIDATOR_ENABLED: |
| return ValidationResult(True, "validator disabled", 1.0) |
| if not text or not text.strip(): |
| return ValidationResult(False, "empty response", 0.0) |
| return ValidationResult(True, "", 1.0) |
|
|