Skip to content

Commit 231c391

Browse files
Create validate-examples.yml
1 parent 596d01c commit 231c391

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
49+
has_error = False
50+
51+
for target in validation_targets:
52+
name = target["name"]
53+
example_path = Path(target["example"])
54+
schema_path = Path(target["schema"])
55+
56+
print(f"Validating {name}...")
57+
print(f" example: {example_path}")
58+
print(f" schema: {schema_path}")
59+
60+
if not example_path.exists():
61+
print(f"ERROR: Example file not found: {example_path}")
62+
has_error = True
63+
continue
64+
65+
if not schema_path.exists():
66+
print(f"ERROR: Schema file not found: {schema_path}")
67+
has_error = True
68+
continue
69+
70+
with schema_path.open("r", encoding="utf-8") as f:
71+
schema = json.load(f)
72+
73+
with example_path.open("r", encoding="utf-8") as f:
74+
example = yaml.safe_load(f)
75+
76+
validator = Draft202012Validator(
77+
schema,
78+
format_checker=FormatChecker()
79+
)
80+
81+
errors = sorted(
82+
validator.iter_errors(example),
83+
key=lambda e: list(e.path)
84+
)
85+
86+
if errors:
87+
has_error = True
88+
print(f"ERROR: {name} validation failed.")
89+
for error in errors:
90+
path = ".".join(str(p) for p in error.path)
91+
if not path:
92+
path = "<root>"
93+
print(f" - {path}: {error.message}")
94+
else:
95+
print(f"OK: {name} passed validation.")
96+
97+
if has_error:
98+
sys.exit(1)
99+
100+
print("All example validations passed.")
101+
PY

0 commit comments

Comments
 (0)