| |
| """Validate model configs and run results against their schemas.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import sys |
| from pathlib import Path |
|
|
| from pipeline_common import REPO_ROOT, basic_validate_config, load_json |
|
|
|
|
| def main() -> int: |
| parser = argparse.ArgumentParser(description=__doc__) |
| parser.add_argument("paths", nargs="+", type=Path) |
| parser.add_argument("--kind", choices=("config", "result"), default="config") |
| args = parser.parse_args() |
| schema_path = REPO_ROOT / ("schemas/model_config.schema.json" if args.kind == "config" else "schemas/run_result.schema.json") |
| schema = load_json(schema_path) |
| try: |
| import jsonschema |
| except ImportError: |
| jsonschema = None |
| failed = 0 |
| for path in args.paths: |
| value = load_json(path) |
| errors = basic_validate_config(value) if args.kind == "config" else [] |
| if jsonschema is not None: |
| validator = jsonschema.Draft202012Validator(schema) |
| errors.extend(error.message for error in validator.iter_errors(value)) |
| if errors: |
| failed += 1 |
| print(json.dumps({"path": str(path), "status": "FAIL", "errors": sorted(set(errors))}, ensure_ascii=False)) |
| else: |
| print(json.dumps({ |
| "path": str(path), "status": "PASS", |
| "validator": "jsonschema" if jsonschema is not None else "stdlib-structural", |
| }, ensure_ascii=False)) |
| return 1 if failed else 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|
|
|