diff --git a/.github/scripts/validate_json.py b/.github/scripts/validate_json.py new file mode 100644 index 0000000..b4b82c3 --- /dev/null +++ b/.github/scripts/validate_json.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Validate that all JSON files under the schema directory are well-formed.""" + +import json +import sys +from pathlib import Path + + +def validate_json_files(schema_dir: str) -> int: + """Validate every *.json file found under schema_dir. + + Returns 0 if all files are valid, 1 if any file is invalid. + """ + has_error = False + json_files = sorted(Path(schema_dir).rglob("*.json")) + + if not json_files: + print(f"No JSON files found under '{schema_dir}'.") + return 0 + + for filepath in json_files: + try: + with open(filepath, encoding="utf-8") as f: + json.load(f) + print(f"VALID: {filepath}") + except (json.JSONDecodeError, OSError, UnicodeDecodeError) as exc: + print(f"INVALID: {filepath} — {exc}", file=sys.stderr) + has_error = True + + return 1 if has_error else 0 + + +if __name__ == "__main__": + directory = sys.argv[1] if len(sys.argv) > 1 else "schema" + sys.exit(validate_json_files(directory)) diff --git a/.github/workflows/validate-json.yml b/.github/workflows/validate-json.yml new file mode 100644 index 0000000..a22bf8f --- /dev/null +++ b/.github/workflows/validate-json.yml @@ -0,0 +1,20 @@ +name: Validate JSON Schema Files + +on: + pull_request: + paths: + - 'schema/**' + - '.github/scripts/validate_json.py' + - '.github/workflows/validate-json.yml' + +jobs: + validate-json: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Validate JSON files in schema directory + run: python3 .github/scripts/validate_json.py schema