File size: 1,760 Bytes
c4e128a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""kdc validate command — metadata validation."""

from __future__ import annotations

import json
from pathlib import Path

import typer
from rich.console import Console
from rich.table import Table

from app.models.document import DocumentMeta
from app.utils.container import Container

app = typer.Typer(help="Validate metadata JSON files against schema.")
console = Console()


@app.command()
def run(
    ctx: typer.Context,
    meta_dir: Path = typer.Option(Path("metadata"), "--meta-dir", help="Metadata directory."),
) -> None:
    """Validate JSON metadata files in META_DIR."""
    container: Container = ctx.obj
    validator = container.schema_validator

    meta_dir = meta_dir.resolve()
    files = list(meta_dir.glob("*.json"))
    if not files:
        console.print(f"[yellow]No metadata files found in {meta_dir}[/yellow]")
        raise typer.Exit()

    table = Table(title="Validation Results")
    table.add_column("File", style="cyan")
    table.add_column("Status")
    table.add_column("Details")

    errors = 0
    for f in sorted(files):
        try:
            data = json.loads(f.read_text(encoding="utf-8"))
            meta = DocumentMeta.model_validate(data)
            res = validator.validate(meta)
            if res.is_valid:
                warn_text = f" ({len(res.warnings)} warnings)" if res.has_warnings else ""
                table.add_row(f.name, "[green]✓ OK[/green]", warn_text)
            else:
                table.add_row(f.name, "[red]✗ FAIL[/red]", "; ".join(res.errors))
                errors += 1
        except Exception as exc:
            table.add_row(f.name, "[red]✗ ERROR[/red]", str(exc))
            errors += 1

    console.print(table)
    if errors > 0:
        raise typer.Exit(1)