Spaces:
Sleeping
Sleeping
File size: 1,547 Bytes
e2ec8a2 bbbfba8 e2ec8a2 bbbfba8 e2ec8a2 bbbfba8 e2ec8a2 | 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 | """Domain errors for validation and export."""
from __future__ import annotations
from enum import StrEnum
from pydantic import BaseModel, ConfigDict, Field
class Severity(StrEnum):
"""Severity level for validation entries."""
ERROR = "error"
WARNING = "warning"
INFO = "info"
class ValidationEntry(BaseModel):
"""A single validation finding."""
model_config = ConfigDict(frozen=True)
validator: str = Field(min_length=1)
severity: Severity
path: str = Field(
min_length=1,
description="Path in the document, e.g. pages[0].text_regions[1].lines[3]",
)
message: str = Field(min_length=1)
code: str | None = None
class ValidationReport(BaseModel):
"""Aggregated results from all validators."""
entries: list[ValidationEntry] = Field(default_factory=list)
@property
def errors(self) -> list[ValidationEntry]:
return [e for e in self.entries if e.severity == Severity.ERROR]
@property
def warnings(self) -> list[ValidationEntry]:
return [e for e in self.entries if e.severity == Severity.WARNING]
@property
def is_valid(self) -> bool:
return len(self.errors) == 0
@property
def error_count(self) -> int:
return len(self.errors)
@property
def warning_count(self) -> int:
return len(self.warnings)
def add(self, entry: ValidationEntry) -> None:
self.entries.append(entry)
def merge(self, other: ValidationReport) -> None:
self.entries.extend(other.entries)
|