Create validate-examples.yml #1
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", | |
| } | |
| ] | |
| 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 | |
| with schema_path.open("r", encoding="utf-8") as f: | |
| schema = json.load(f) | |
| with example_path.open("r", encoding="utf-8") as f: | |
| example = yaml.safe_load(f) | |
| validator = Draft202012Validator( | |
| schema, | |
| format_checker=FormatChecker() | |
| ) | |
| errors = sorted( | |
| validator.iter_errors(example), | |
| key=lambda e: list(e.path) | |
| ) | |
| if errors: | |
| has_error = True | |
| print(f"ERROR: {name} validation failed.") | |
| for error in errors: | |
| path = ".".join(str(p) for p 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 |