Update README.md #16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Validate Examples | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| validate-examples: | |
| name: Validate YAML examples against JSON Schemas | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v6 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: "3.12" | |
| - name: Install validation dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install jsonschema PyYAML | |
| - name: Validate examples | |
| run: | | |
| python <<'PY' | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import yaml | |
| from jsonschema import Draft202012Validator, FormatChecker | |
| validation_targets = [ | |
| { | |
| "name": "Purity Assessment", | |
| "example": "examples/purity-assessment.sample.yaml", | |
| "schema": "schemas/purity-assessment.schema.json", | |
| }, | |
| { | |
| "name": "Purity Assessment Low Confidence", | |
| "example": "examples/purity-assessment.low-confidence.sample.yaml", | |
| "schema": "schemas/purity-assessment.schema.json", | |
| }, | |
| { | |
| "name": "Purity Assessment Recursive Synthetic Risk", | |
| "example": "examples/purity-assessment.recursive-synthetic-risk.sample.yaml", | |
| "schema": "schemas/purity-assessment.schema.json", | |
| }, | |
| ] | |
| has_error = False | |
| for target in validation_targets: | |
| name = target["name"] | |
| example_path = Path(target["example"]) | |
| schema_path = Path(target["schema"]) | |
| print(f"Validating {name}...") | |
| print(f" example: {example_path}") | |
| print(f" schema: {schema_path}") | |
| if not example_path.exists(): | |
| print(f"ERROR: Example file not found: {example_path}") | |
| has_error = True | |
| continue | |
| if not schema_path.exists(): | |
| print(f"ERROR: Schema file not found: {schema_path}") | |
| has_error = True | |
| continue | |
| try: | |
| with schema_path.open("r", encoding="utf-8") as f: | |
| schema = json.load(f) | |
| except Exception as exc: | |
| print(f"ERROR: Failed to load schema file: {schema_path}") | |
| print(f" {exc}") | |
| has_error = True | |
| continue | |
| try: | |
| with example_path.open("r", encoding="utf-8") as f: | |
| example = yaml.safe_load(f) | |
| except Exception as exc: | |
| print(f"ERROR: Failed to load YAML example file: {example_path}") | |
| print(f" {exc}") | |
| has_error = True | |
| continue | |
| validator = Draft202012Validator( | |
| schema, | |
| format_checker=FormatChecker() | |
| ) | |
| errors = sorted( | |
| validator.iter_errors(example), | |
| key=lambda error: list(error.path) | |
| ) | |
| if errors: | |
| has_error = True | |
| print(f"ERROR: {name} validation failed.") | |
| for error in errors: | |
| path = ".".join(str(part) for part in error.path) | |
| if not path: | |
| path = "<root>" | |
| print(f" - {path}: {error.message}") | |
| else: | |
| print(f"OK: {name} passed validation.") | |
| if has_error: | |
| sys.exit(1) | |
| print("All example validations passed.") | |
| PY |