File size: 1,428 Bytes
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
"""Schema validator — validates a dict/JSON against the CanonicalDocument schema.

This wraps Pydantic validation as an explicit service, producing a
ValidationReport rather than raising exceptions.
"""

from __future__ import annotations

from typing import Any

from pydantic import ValidationError

from src.app.domain.errors import Severity, ValidationEntry, ValidationReport
from src.app.domain.models import CanonicalDocument

VALIDATOR_NAME = "schema"


def validate_schema(data: dict[str, Any]) -> tuple[CanonicalDocument | None, ValidationReport]:
    """Validate raw data against the CanonicalDocument schema.

    Returns:
        A tuple of (parsed document or None, validation report).
        If parsing succeeds, the document is returned with an empty report.
        If parsing fails, None is returned with errors in the report.
    """
    report = ValidationReport()
    try:
        doc = CanonicalDocument.model_validate(data)
        return doc, report
    except ValidationError as e:
        for error in e.errors():
            loc_parts = [str(p) for p in error["loc"]]
            path = ".".join(loc_parts) if loc_parts else "root"
            report.add(ValidationEntry(
                validator=VALIDATOR_NAME,
                severity=Severity.ERROR,
                path=path,
                message=error["msg"],
                code=error["type"],
            ))
        return None, report