from __future__ import annotations from src.domain import MAX_BYTES, MAX_CHARS, MAX_DOCS, MAX_FILES, MAX_PAGES from src.schemas import LabDocument, ValidationReport def validate_text(name: str, text: str, report: ValidationReport) -> None: if text is None: report.add("empty", "Text is missing.", name) return if len(text) > MAX_CHARS: report.add("too_large_text", f"Text exceeds {MAX_CHARS} characters.", name) if "\x00" in text: report.add("binary", "Binary content is out of scope.", name) lowered = text.lower() if "begin private key" in lowered or "aws_secret_access_key" in lowered: report.add("credential_rejected", "Live credentials are not accepted in the public demo.", name) def validate_documents(documents: list[LabDocument]) -> ValidationReport: report = ValidationReport(ok=True) if not documents: report.add("empty", "No laboratory document is selected.", "documents") return report if len(documents) > MAX_DOCS: report.add("too_many_docs", f"At most {MAX_DOCS} documents are scored.", "documents") total = 0 for item in documents: validate_text(item.doc_id, item.body, report) total += len(item.body.encode("utf-8", errors="replace")) if total > MAX_BYTES: report.add("too_large", f"Corpus exceeds {MAX_BYTES} bytes.", "documents") return report def validate_limits(n_files: int, pages: int, chars: int, report: ValidationReport) -> None: if n_files > MAX_FILES: report.add("too_many_files", f"At most {MAX_FILES} uploads.", "uploads") if pages > MAX_PAGES: report.add("too_many_pages", f"PDFs are limited to {MAX_PAGES} pages.", "pdf") if chars > MAX_CHARS: report.add("too_large_text", f"Combined text exceeds {MAX_CHARS} characters.", "text")