darachhat
feat: build production-ready Khmer Document Corpus v0.2.0 with Typer CLI, PyMuPDF, Polars, and DI architecture
c4e128a
Raw
History Blame Contribute Delete
1.76 kB
"""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)