-
Notifications
You must be signed in to change notification settings - Fork 0
463 lines (391 loc) · 17.5 KB
/
Copy pathvalidations.yml
File metadata and controls
463 lines (391 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
name: Validate Schemas and Examples
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Need full history to compare changes
- name: Get changed files and determine validation needs
id: analysis
run: |
# Determine the base commit for comparison
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
else
BASE_SHA="${{ github.event.before }}"
if [ "$BASE_SHA" = "0000000000000000000000000000000000000000" ]; then
BASE_SHA="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
fi
fi
echo "Comparing against: $BASE_SHA"
# Get all changed files and filter out archived
# git diff --name-only --diff-filter=AMR "$BASE_SHA" HEAD | \
# grep -v 'specs-core/archived/' > /tmp/all_changed_files.txt || true
# Toggle: set to "true" to validate all files, "false" for changed files only
VALIDATE_ALL_FILES="false"
if [ "$VALIDATE_ALL_FILES" = "true" ]; then
# Get all files and filter out .vscode
find . -type f \( -name "*.json" -o -name "*.jsonld" -o -name "*.cddl" -o -name "*.cbor" -o -name "*.cbordiag" \) \
-not -path "*/.vscode/*" \
-not -path "./.git/*" \
-not -path "./node_modules/*" > /tmp/all_changed_files.txt || true
else
# Get all changed files and filter out .vscode
git diff --name-only --diff-filter=AMR "$BASE_SHA" HEAD | \
grep -v '\.vscode/' > /tmp/all_changed_files.txt || true
fi
# Categorize changed files
grep '\.json$' /tmp/all_changed_files.txt > /tmp/json_files.txt || true
grep '\.jsonld$' /tmp/all_changed_files.txt > /tmp/jsonld_files.txt || true
grep '\.cddl$' /tmp/all_changed_files.txt > /tmp/cddl_files.txt || true
grep -E '\.(cbor|cbordiag)$' /tmp/all_changed_files.txt > /tmp/cbor_files.txt || true
# Determine what tools we need
NEED_JSON_TOOLS="false"
NEED_OPENAPI_TOOLS="false"
NEED_JSONLD_TOOLS="false"
NEED_CDDL_TOOLS="false"
# Check if we need JSON Schema validation
if [ -s /tmp/json_files.txt ]; then
NEED_JSON_TOOLS="true"
# Check if any are OpenAPI files
while read file; do
if [ -f "$file" ] && ([[ "$file" =~ \.openapi\.json$ ]] || grep -q '"openapi".*"3\.' "$file" 2>/dev/null); then
NEED_OPENAPI_TOOLS="true"
break
fi
done < /tmp/json_files.txt
fi
# Check if we need JSON-LD tools
if [ -s /tmp/jsonld_files.txt ]; then
NEED_JSONLD_TOOLS="true"
fi
# Check if we need CDDL tools
if [ -s /tmp/cddl_files.txt ] || [ -s /tmp/cbor_files.txt ]; then
NEED_CDDL_TOOLS="true"
fi
# Output results
echo "need_json_tools=$NEED_JSON_TOOLS" >> $GITHUB_OUTPUT
echo "need_openapi_tools=$NEED_OPENAPI_TOOLS" >> $GITHUB_OUTPUT
echo "need_jsonld_tools=$NEED_JSONLD_TOOLS" >> $GITHUB_OUTPUT
echo "need_cddl_tools=$NEED_CDDL_TOOLS" >> $GITHUB_OUTPUT
# Show what we found
echo "📋 Analysis Results:"
echo "JSON files changed: $(wc -l < /tmp/json_files.txt)"
echo "JSON-LD files changed: $(wc -l < /tmp/jsonld_files.txt)"
echo "CDDL files changed: $(wc -l < /tmp/cddl_files.txt)"
echo "CBOR files changed: $(wc -l < /tmp/cbor_files.txt)"
echo ""
echo "Tools needed:"
echo "- JSON Schema: $NEED_JSON_TOOLS"
echo "- OpenAPI: $NEED_OPENAPI_TOOLS"
echo "- JSON-LD: $NEED_JSONLD_TOOLS"
echo "- CDDL: $NEED_CDDL_TOOLS"
- name: Setup Node.js
if: steps.analysis.outputs.need_json_tools == 'true' || steps.analysis.outputs.need_openapi_tools == 'true' || steps.analysis.outputs.need_jsonld_tools == 'true' || steps.analysis.outputs.need_cddl_tools == 'true'
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Setup Rust
if: steps.analysis.outputs.need_cddl_tools == 'true'
uses: dtolnay/rust-toolchain@stable
- name: Install JSON Schema tools
if: steps.analysis.outputs.need_json_tools == 'true'
uses: sourcemeta/jsonschema@main
- name: Install OpenAPI tools
if: steps.analysis.outputs.need_openapi_tools == 'true'
run: npm install -g @redocly/cli
- name: Install JSON-LD tools
if: steps.analysis.outputs.need_jsonld_tools == 'true'
run: |
npm install -g jsonld-cli
- name: Install CDDL tools
if: steps.analysis.outputs.need_cddl_tools == 'true'
run: |
npm install -g cddl
cargo install cbor-diag-cli
- name: Validate JSON files
if: steps.analysis.outputs.need_json_tools == 'true'
run: |
echo "🔍 Processing JSON files..."
exit_code=0
# Track validated pairs to avoid duplicates
validated_pairs_file="/tmp/validated_pairs.txt"
> "$validated_pairs_file" # Clear the file
# Function to normalize paths (remove leading ./)
normalize_path() {
echo "$1" | sed 's|^\./||'
}
while read json_file; do
if [ ! -f "$json_file" ]; then
continue
fi
echo "Processing: $json_file"
# Check if this is a schema file
if [[ "$json_file" =~ \.schema\.json$ ]]; then
# 1. Lint schema files only
echo " Debug: About to lint schema file"
echo " Debug: jsonschema version:"
jsonschema --version || echo "No version flag available"
echo " Debug: Current working directory: $(pwd)"
echo " Debug: File exists check: $(ls -la "$json_file")"
if ! jsonschema lint "$json_file"; then
echo "❌ Lint failed: $json_file"
exit_code=1
continue
fi
echo "✅ Lint passed: $json_file"
# 2. Find and validate instances against this schema
base_name=$(basename "$json_file" .schema.json)
echo " Found schema: $base_name"
# Find ALL matching instances in the entire tree
instances=$(find . -name "${base_name}-*.json" \
-not -name "*.schema.json" \
-not -name "*.openapi.json" \
-not -path "./specs-core/archived/*" \
-type f)
if [ -z "$instances" ]; then
echo " ⚠️ No instances found for schema: $json_file"
else
while read instance; do
if [ -n "$instance" ]; then
# Normalize paths for consistent comparison
norm_schema=$(normalize_path "$json_file")
norm_instance=$(normalize_path "$instance")
pair_id="$norm_schema::$norm_instance"
# Check if we've already validated this pair
if ! grep -Fxq "$pair_id" "$validated_pairs_file"; then
echo " Validating $instance against $json_file"
if ! jsonschema validate "$json_file" "$instance"; then
echo " ❌ Validation failed: $instance"
exit_code=1
else
echo " ✅ Valid: $instance"
fi
# Record that we've validated this pair
echo "$pair_id" >> "$validated_pairs_file"
else
echo " ℹ️ Already validated: $instance"
fi
fi
done <<< "$instances"
fi
# Check if this is an instance file that needs validation
elif [[ "$json_file" =~ -.*\.json$ ]] && [[ ! "$json_file" =~ \.openapi\.json$ ]]; then
# No linting for instances, just validation against schema
# Extract potential schema name by removing the last part after the last hyphen
file_base=$(basename "$json_file")
# Remove .json extension first
file_base_no_ext="${file_base%.json}"
# Find the last hyphen and remove everything after it
if [[ "$file_base_no_ext" =~ ^(.+)-[^-]+$ ]]; then
schema_base="${BASH_REMATCH[1]}"
schema_name="${schema_base}.schema.json"
echo " Looking for schema: $schema_name"
# Find the schema anywhere in the tree
schema_file=$(find . -name "$schema_name" \
-not -path "./specs-core/archived/*" \
-type f | head -1)
echo " Debug: find command result: '$schema_file'"
if [ -n "$schema_file" ] && [ -f "$schema_file" ]; then
echo " Found schema: $schema_file"
# Normalize paths for consistent comparison
norm_schema=$(normalize_path "$schema_file")
norm_instance=$(normalize_path "$json_file")
pair_id="$norm_schema::$norm_instance"
echo " Debug: Checking if already validated: $pair_id"
# Check if we've already validated this pair
if ! grep -Fxq "$pair_id" "$validated_pairs_file"; then
echo " Validating $json_file against $schema_file"
if ! jsonschema validate "$schema_file" "$json_file"; then
echo " ❌ Validation failed: $json_file"
exit_code=1
else
echo " ✅ Valid: $json_file"
fi
# Record that we've validated this pair
echo "$pair_id" >> "$validated_pairs_file"
else
echo " ℹ️ Already validated: $json_file"
fi
else
echo " ⚠️ No schema found for: $json_file"
echo " Debug: Searched for file: $schema_name"
echo " Debug: Find result was empty or file doesn't exist"
fi
else
echo " ⚠️ Cannot extract schema name from: $json_file"
echo " Debug: Regex didn't match pattern"
fi
# Handle other JSON files (non-schema, non-instance patterns)
else
# Only lint JSON files that are not OpenAPI files
if [[ ! "$json_file" =~ \.openapi\.json$ ]]; then
if ! jsonschema lint "$json_file"; then
echo "❌ Lint failed: $json_file"
exit_code=1
else
echo "✅ Lint passed: $json_file"
fi
else
echo " ℹ️ Skipping OpenAPI file (handled separately): $json_file"
fi
fi
done < /tmp/json_files.txt
if [ $exit_code -ne 0 ]; then
#exit 1
exit 0 # For testing purposes, we don't want to fail the workflow
fi
- name: Validate OpenAPI files
if: steps.analysis.outputs.need_openapi_tools == 'true'
run: |
echo "🔍 Validating OpenAPI files..."
exit_code=0
while read json_file; do
if [ -f "$json_file" ]; then
if [[ "$json_file" =~ \.openapi\.json$ ]] || grep -q '"openapi".*"3\.' "$json_file" 2>/dev/null; then
echo "Validating OpenAPI: $json_file"
if ! redocly lint "$json_file"; then
echo "❌ OpenAPI validation failed: $json_file"
exit_code=1
else
echo "✅ Valid OpenAPI: $json_file"
fi
fi
fi
done < /tmp/json_files.txt
if [ $exit_code -ne 0 ]; then
exit 1
fi
- name: Validate JSON-LD files
if: steps.analysis.outputs.need_jsonld_tools == 'true'
run: |
echo "🔍 Validating JSON-LD files..."
exit_code=0
while read jsonld_file; do
if [ -f "$jsonld_file" ]; then
echo "Validating JSON-LD: $jsonld_file"
# Step 1: Basic JSON syntax check
if ! jq empty "$jsonld_file" 2>/dev/null; then
echo "❌ Invalid JSON syntax: $jsonld_file"
exit_code=1
continue
fi
# Step 2: JSON-LD expansion test (validates JSON-LD structure)
if ! jsonld expand "$jsonld_file" > /dev/null 2>&1; then
echo "❌ JSON-LD expansion failed: $jsonld_file"
exit_code=1
continue
fi
# Step 3: Check for required JSON-LD elements
if ! jq -e 'has("@context")' "$jsonld_file" > /dev/null 2>&1; then
echo "⚠️ Warning: JSON-LD file missing @context: $jsonld_file"
fi
echo "✅ Valid JSON-LD: $jsonld_file"
fi
done < /tmp/jsonld_files.txt
if [ $exit_code -ne 0 ]; then
exit 1
fi
- name: Validate CDDL files
if: steps.analysis.outputs.need_cddl_tools == 'true'
run: |
echo "🔍 Validating CDDL files..."
exit_code=0
# Check CDDL syntax
if [ -s /tmp/cddl_files.txt ]; then
while read cddl_file; do
if [ -f "$cddl_file" ]; then
echo "Checking CDDL syntax: $cddl_file"
if ! npx cddl validate "$cddl_file"; then
echo "❌ CDDL validation failed: $cddl_file"
exit_code=1
else
echo "✅ Valid CDDL syntax: $cddl_file"
fi
fi
done < /tmp/cddl_files.txt
fi
# Validate CBOR files if any CDDL or CBOR files changed
if [ -s /tmp/cddl_files.txt ] || [ -s /tmp/cbor_files.txt ]; then
echo "Running CBOR validation..."
for cddl_dir in $(find . -name "*.cddl" -not -path "./specs-core/archived/*" | xargs dirname | sort -u); do
cd "$cddl_dir"
# Create common.cddl if common CDDL files exist
common_files=()
for common_file in hashed-ext-uri.cddl hashed-uri.cddl assertion-metadata-common.cddl max-tstr-length.cddl; do
if [ -f "$common_file" ]; then
common_files+=("$common_file")
fi
done
if [ ${#common_files[@]} -gt 0 ]; then
echo "Creating common.cddl in $cddl_dir"
for f in "${common_files[@]}"; do
cat "$f"
echo ""
done > common.cddl
fi
# Generate CBOR from .cbordiag files if they exist
for cbordiag_file in $(find . -name "*.cbordiag" 2>/dev/null || true); do
if [ -f "$cbordiag_file" ]; then
cbor_file="${cbordiag_file%.cbordiag}.cbor"
echo "Generating $cbor_file from $cbordiag_file"
cbor-diag-cli convert --input "$cbordiag_file" --output "$cbor_file" || true
fi
done
# Validate CBOR examples against their schemas
for cbor_file in $(find . -name "*.cbor" 2>/dev/null || true); do
if [ -f "$cbor_file" ]; then
base_name=$(basename "$cbor_file" .cbor)
schema_file="${base_name}.cddl"
if [ -f "$schema_file" ]; then
echo "Validating $cbor_file against $schema_file"
if [ -f "common.cddl" ]; then
cat "$schema_file" common.cddl | npx cddl - validate "$cbor_file"
else
npx cddl "$schema_file" validate "$cbor_file"
fi
if [ $? -ne 0 ]; then
echo "❌ CBOR validation failed: $cbor_file"
exit_code=1
else
echo "✅ Valid CBOR: $cbor_file"
fi
fi
fi
done
# Clean up
[ -f "common.cddl" ] && rm common.cddl
cd - > /dev/null
done
fi
if [ $exit_code -ne 0 ]; then
exit 1
fi
- name: Summary
if: always()
run: |
echo "🎯 Validation Summary:"
if [ -s /tmp/json_files.txt ]; then
echo "JSON files processed: $(wc -l < /tmp/json_files.txt)"
fi
if [ -s /tmp/jsonld_files.txt ]; then
echo "JSON-LD files processed: $(wc -l < /tmp/jsonld_files.txt)"
fi
if [ -s /tmp/cddl_files.txt ]; then
echo "CDDL files processed: $(wc -l < /tmp/cddl_files.txt)"
fi
if [ -s /tmp/cbor_files.txt ]; then
echo "CBOR files processed: $(wc -l < /tmp/cbor_files.txt)"
fi
if [ ! -s /tmp/json_files.txt ] && [ ! -s /tmp/jsonld_files.txt ] && [ ! -s /tmp/cddl_files.txt ] && [ ! -s /tmp/cbor_files.txt ]; then
echo "ℹ️ No relevant files changed - validation skipped"
fi