|
| 1 | +name: Validate Examples |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + validate-examples: |
| 14 | + name: Validate YAML examples against JSON Schemas |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Check out repository |
| 19 | + uses: actions/checkout@v6 |
| 20 | + |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v6 |
| 23 | + with: |
| 24 | + python-version: "3.12" |
| 25 | + |
| 26 | + - name: Install validation dependencies |
| 27 | + run: | |
| 28 | + python -m pip install --upgrade pip |
| 29 | + pip install jsonschema PyYAML |
| 30 | +
|
| 31 | + - name: Validate examples |
| 32 | + run: | |
| 33 | + python <<'PY' |
| 34 | + import json |
| 35 | + import sys |
| 36 | + from pathlib import Path |
| 37 | +
|
| 38 | + import yaml |
| 39 | + from jsonschema import Draft202012Validator, FormatChecker |
| 40 | +
|
| 41 | + validation_targets = [ |
| 42 | + { |
| 43 | + "name": "Purity Assessment", |
| 44 | + "example": "examples/purity-assessment.sample.yaml", |
| 45 | + "schema": "schemas/purity-assessment.schema.json", |
| 46 | + }, |
| 47 | + { |
| 48 | + "name": "Purity Assessment Low Confidence", |
| 49 | + "example": "examples/purity-assessment.low-confidence.sample.yaml", |
| 50 | + "schema": "schemas/purity-assessment.schema.json", |
| 51 | + }, |
| 52 | + { |
| 53 | + "name": "Purity Assessment Recursive Synthetic Risk", |
| 54 | + "example": "examples/purity-assessment.recursive-synthetic-risk.sample.yaml", |
| 55 | + "schema": "schemas/purity-assessment.schema.json", |
| 56 | + }, |
| 57 | + ] |
| 58 | +
|
| 59 | + has_error = False |
| 60 | +
|
| 61 | + for target in validation_targets: |
| 62 | + name = target["name"] |
| 63 | + example_path = Path(target["example"]) |
| 64 | + schema_path = Path(target["schema"]) |
| 65 | +
|
| 66 | + print(f"Validating {name}...") |
| 67 | + print(f" example: {example_path}") |
| 68 | + print(f" schema: {schema_path}") |
| 69 | +
|
| 70 | + if not example_path.exists(): |
| 71 | + print(f"ERROR: Example file not found: {example_path}") |
| 72 | + has_error = True |
| 73 | + continue |
| 74 | +
|
| 75 | + if not schema_path.exists(): |
| 76 | + print(f"ERROR: Schema file not found: {schema_path}") |
| 77 | + has_error = True |
| 78 | + continue |
| 79 | +
|
| 80 | + try: |
| 81 | + with schema_path.open("r", encoding="utf-8") as f: |
| 82 | + schema = json.load(f) |
| 83 | + except Exception as exc: |
| 84 | + print(f"ERROR: Failed to load schema file: {schema_path}") |
| 85 | + print(f" {exc}") |
| 86 | + has_error = True |
| 87 | + continue |
| 88 | +
|
| 89 | + try: |
| 90 | + with example_path.open("r", encoding="utf-8") as f: |
| 91 | + example = yaml.safe_load(f) |
| 92 | + except Exception as exc: |
| 93 | + print(f"ERROR: Failed to load YAML example file: {example_path}") |
| 94 | + print(f" {exc}") |
| 95 | + has_error = True |
| 96 | + continue |
| 97 | +
|
| 98 | + validator = Draft202012Validator( |
| 99 | + schema, |
| 100 | + format_checker=FormatChecker() |
| 101 | + ) |
| 102 | +
|
| 103 | + errors = sorted( |
| 104 | + validator.iter_errors(example), |
| 105 | + key=lambda error: list(error.path) |
| 106 | + ) |
| 107 | +
|
| 108 | + if errors: |
| 109 | + has_error = True |
| 110 | + print(f"ERROR: {name} validation failed.") |
| 111 | +
|
| 112 | + for error in errors: |
| 113 | + path = ".".join(str(part) for part in error.path) |
| 114 | + if not path: |
| 115 | + path = "<root>" |
| 116 | +
|
| 117 | + print(f" - {path}: {error.message}") |
| 118 | + else: |
| 119 | + print(f"OK: {name} passed validation.") |
| 120 | +
|
| 121 | + if has_error: |
| 122 | + sys.exit(1) |
| 123 | +
|
| 124 | + print("All example validations passed.") |
| 125 | + PY |
0 commit comments