Skip to content

Commit 1d982c8

Browse files
committed
Update workflow
1 parent da5dfb9 commit 1d982c8

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

.github/workflows/validate-schema.yml

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,60 @@ jobs:
1717
runs-on: ubuntu-latest
1818

1919
steps:
20-
- name: Checkout
20+
- name: Checkout repo
2121
uses: actions/checkout@v4
2222

23-
- name: Use Node 20
23+
- name: Use Node.js 20
2424
uses: actions/setup-node@v4
2525
with:
2626
node-version: "20"
2727

28-
# Install AJV CLI and formats (CJS-compatible)
2928
- name: Install AJV CLI and formats
3029
run: |
31-
npm install --no-save ajv-cli@5.0.0 ajv-formats@1.6.1
30+
npm install --no-save ajv@8 ajv-formats@2
3231
33-
# 1) Meta-validate the schema
34-
- name: Compile (meta-validate) schema
32+
- name: Validate schema examples (with ajv-formats)
3533
run: |
36-
npx ajv compile \
37-
--spec=draft7 \
38-
--allow-union-types \
39-
-c ajv-formats \
40-
-s schema/gptp.schema.json
34+
node <<'EOF'
35+
const fs = require('fs');
36+
const path = require('path');
37+
const Ajv = require('ajv').default;
38+
const addFormats = require('ajv-formats');
4139
42-
# 2) Validate all example .gptp files (if any)
43-
- name: Validate examples against schema
44-
run: |
45-
files=$(find docs/examples -name '*.gptp')
46-
if [ -z "$files" ]; then
47-
echo "No .gptp files in docs/examples/. Skipping data validation."
48-
exit 0
49-
fi
40+
const ajv = new Ajv({ strict: true, allowUnionTypes: true });
41+
addFormats(ajv);
42+
43+
const schema = JSON.parse(fs.readFileSync('schema/gptp.schema.json', 'utf8'));
44+
const validate = ajv.compile(schema);
45+
46+
const exampleDir = 'docs/examples';
47+
const files = fs.readdirSync(exampleDir).filter(f => f.endsWith('.gptp'));
48+
49+
if (files.length === 0) {
50+
console.log("No .gptp files found. Skipping.");
51+
process.exit(0);
52+
}
53+
54+
let failures = 0;
5055
51-
echo "Validating the following files:"
52-
echo "$files"
56+
for (const file of files) {
57+
const filePath = path.join(exampleDir, file);
58+
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
5359
54-
datalist=$(echo "$files" | xargs -n1 printf -- "-d %s ")
60+
const valid = validate(data);
61+
if (!valid) {
62+
console.error(`❌ ${file} failed validation:`);
63+
console.error(validate.errors);
64+
failures++;
65+
} else {
66+
console.log(`✅ ${file} passed`);
67+
}
68+
}
5569
56-
eval npx ajv validate \
57-
--spec=draft7 \
58-
--allow-union-types \
59-
-c ajv-formats \
60-
-s schema/gptp.schema.json \
61-
$datalist
70+
if (failures > 0) {
71+
process.exit(1);
72+
}
73+
EOF
6274
63-
# Optional cleanup
6475
- name: Cleanup
6576
run: rm -rf node_modules package-lock.json

0 commit comments

Comments
 (0)