-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmetadata-validate.sh
More file actions
executable file
·508 lines (454 loc) · 20.2 KB
/
metadata-validate.sh
File metadata and controls
executable file
·508 lines (454 loc) · 20.2 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/bin/bash
##################################################
# Colors
#BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BRIGHTWHITE='\033[0;37;1m'
NC='\033[0m'
UNDERLINE='\033[4m'
BOLD='\033[1m'
GRAY='\033[0;90m'
WHITE='\033[1;37m'
##################################################
# Default schema URLs
CIP_100_SCHEMA="https://raw.githubusercontent.com/cardano-foundation/CIPs/refs/heads/master/CIP-0100/cip-0100.common.schema.json"
CIP_108_SCHEMA="https://raw.githubusercontent.com/cardano-foundation/CIPs/refs/heads/master/CIP-0108/cip-0108.common.schema.json"
CIP_119_SCHEMA="https://raw.githubusercontent.com/cardano-foundation/CIPs/refs/heads/master/CIP-0119/cip-0119.common.schema.json"
CIP_136_SCHEMA="https://raw.githubusercontent.com/cardano-foundation/CIPs/refs/heads/master/CIP-0136/cip-136.common.schema.json"
CIP_169_SCHEMA="https://raw.githubusercontent.com/elenabardho/CIPs/refs/heads/cip-governance-metadata-extension-schema/cip-governance-metadata-extension/cip-0169.common.schema.json"
INTERSECT_TREASURY_SCHEMA="https://raw.githubusercontent.com/IntersectMBO/governance-actions/refs/heads/main/schemas/treasury-withdrawals/common.schema.json"
INTERSECT_INFO_SCHEMA="https://raw.githubusercontent.com/IntersectMBO/governance-actions/refs/heads/main/schemas/info/common.schema.json"
INTERSECT_PPU_SCHEMA="https://raw.githubusercontent.com/IntersectMBO/governance-actions/refs/heads/main/schemas/parameter-changes/common.schema.json"
# Default aspell dictionary (fetched at runtime so users don't need a local copy)
CARDANO_ASPELL_DICT_URL="https://raw.githubusercontent.com/IntersectMBO/governance-scripts/refs/heads/main/scripts/cardano-aspell-dict.txt"
# Default schema values
DEFAULT_USE_CIP_100="false"
DEFAULT_USE_CIP_108="false"
DEFAULT_USE_CIP_119="false"
DEFAULT_USE_CIP_136="false"
DEFAULT_USE_CIP_169="false"
DEFAULT_USE_INTERSECT="false"
##################################################
# Check if cardano-signer is installed
if ! command -v cardano-signer >/dev/null 2>&1; then
echo -e "${RED}Error: cardano-signer is not installed or not in your PATH.${NC}" >&2
exit 1
fi
# Check if ajv is installed
if ! command -v ajv >/dev/null 2>&1; then
echo -e "${RED}Error: ajv is not installed or not in your PATH.${NC}" >&2
exit 1
fi
set -euo pipefail
# Global variables for cleanup
TMP_JSON_FILE=""
TMP_SCHEMAS_DIR="/tmp/schemas"
TMP_DICT_FILE=""
# Cleanup function
cleanup() {
# Clean up temporary JSON file if it exists
if [ -n "$TMP_JSON_FILE" ] && [ -f "$TMP_JSON_FILE" ]; then
rm -f "$TMP_JSON_FILE" 2>/dev/null || true
fi
# Clean up temporary schemas directory if it exists
if [ -d "$TMP_SCHEMAS_DIR" ]; then
rm -rf "$TMP_SCHEMAS_DIR" 2>/dev/null || true
fi
# Clean up downloaded aspell dictionary
if [ -n "$TMP_DICT_FILE" ] && [ -f "$TMP_DICT_FILE" ]; then
rm -f "$TMP_DICT_FILE" 2>/dev/null || true
fi
}
# Set up trap to ensure cleanup on exit
trap cleanup EXIT INT TERM
# Usage message
usage() {
local col=50
echo -e "${UNDERLINE}${BOLD}Validate a JSON-LD metadata file${NC}"
echo -e "\n"
echo -e "Syntax:${BOLD} $0 ${GREEN}<jsonld-file> ${NC}[${GREEN}--cip169${NC}] [${GREEN}--cip108${NC}] [${GREEN}--cip100${NC}] [${GREEN}--cip136${NC}] [${GREEN}--intersect-schema${NC}] [${GREEN}--schema ${NC}URL] [${GREEN}--no-spell-check${NC}] [${GREEN}--no-check-links${NC}] [${GREEN}--draft${NC}]"
printf "Params: ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "<jsonld-file>" "- Path to the JSON-LD metadata file"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--cip100]" "- Compare against CIP-100 schema (default: $DEFAULT_USE_CIP_100)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--cip108]" "- Compare against CIP-108 Governance actions schema (default: $DEFAULT_USE_CIP_108)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--cip119]" "- Compare against CIP-119 DRep schema (default: $DEFAULT_USE_CIP_119)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--cip136]" "- Compare against CIP-136 CC vote schema (default: $DEFAULT_USE_CIP_136)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--cip169]" "- Compare against CIP-169 Governance metadata schema (default: $DEFAULT_USE_CIP_169, includes CIP-116)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--intersect-schema]" "- Compare against Intersect governance action schemas (default: $DEFAULT_USE_INTERSECT)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--schema URL]" "- Compare against schema at URL"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-spell-check]" "- Skip aspell-based spell check on body.title/abstract/motivation/rationale (default: enabled; dictionary fetched from IntersectMBO/governance-scripts main)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-check-links]" "- Skip URI reachability check on body URIs and prose markdown links (default: enabled; IPFS gateway via \$IPFS_GATEWAY_URI, falls back to https://ipfs.io)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--draft]" "- Treat the file as a pre-signing draft: downgrade the empty-authors check to a warning instead of an error."
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "-h, --help" "- Show this help message and exit"
exit 1
}
# Initialize variables with defaults
input_file=""
use_cip_108="$DEFAULT_USE_CIP_108"
use_cip_100="$DEFAULT_USE_CIP_100"
use_cip_119="$DEFAULT_USE_CIP_119"
use_cip_136="$DEFAULT_USE_CIP_136"
use_cip_169="$DEFAULT_USE_CIP_169"
use_intersect_schema="$DEFAULT_USE_INTERSECT"
user_schema_url=""
user_schema="false"
check_links="true"
check_spelling="true"
is_draft="false"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--cip108|--cip-108)
use_cip_108="true"
shift
;;
--cip100|--cip-100)
use_cip_100="true"
shift
;;
--cip119|--cip-119)
use_cip_119="true"
shift
;;
--cip136|--cip-136)
use_cip_136="true"
shift
;;
--cip169|--cip-169)
use_cip_169="true"
shift
;;
--intersect-schema)
use_intersect_schema="true"
shift
;;
--schema)
user_schema_url="$2"
user_schema="true"
shift 2
;;
--no-check-links)
check_links="false"
shift
;;
--no-spell-check)
check_spelling="false"
shift
;;
--draft)
is_draft="true"
shift
;;
-h|--help)
usage
;;
*)
if [ -z "$input_file" ]; then
input_file="$1"
fi
shift
;;
esac
done
# Require at least one schema source. Without it the downstream "no schemas downloaded"
# branch would fail anyway, but that happens after length/spell/URI checks run pointlessly.
if [ "$use_cip_100" = "false" ] && [ "$use_cip_108" = "false" ] && \
[ "$use_cip_119" = "false" ] && [ "$use_cip_136" = "false" ] && \
[ "$use_cip_169" = "false" ] && [ "$use_intersect_schema" = "false" ] && \
[ "$user_schema" = "false" ]; then
echo -e "${RED}Error: At least one schema flag is required.${NC}" >&2
echo -e "${YELLOW}Pass one or more of: ${GREEN}--cip100${YELLOW} / ${GREEN}--cip108${YELLOW} / ${GREEN}--cip119${YELLOW} / ${GREEN}--cip136${YELLOW} / ${GREEN}--cip169${YELLOW}, or ${GREEN}--intersect-schema${YELLOW}, or ${GREEN}--schema <URL>${NC}." >&2
usage
fi
# Welcome message
echo -e " "
echo -e "${YELLOW}Governance Metadata Validation Script${NC}"
echo -e "${CYAN}This script validates JSON-LD governance metadata files against CIP standards and or Intersect schemas${NC}"
# If the file ends with .jsonld, create a temporary .json copy (overwrite if exists)
if [[ "$input_file" == *.jsonld ]]; then
if [ ! -f "$input_file" ]; then
echo -e "${RED}Error: File '${YELLOW}$input_file${RED}' does not exist.${NC}">&2
usage
exit 1
fi
TMP_JSON_FILE="/tmp/metadata.json"
cp -f "$input_file" "$TMP_JSON_FILE"
JSON_FILE="$TMP_JSON_FILE"
else
JSON_FILE="$input_file"
fi
# Check if the file exists
if [ ! -f "$JSON_FILE" ]; then
echo -e "${RED}Error: File '${YELLOW}$JSON_FILE${RED}' does not exist.${NC}">&2
usage
exit 1
fi
# Check if the file is valid JSON
if ! jq empty "$JSON_FILE" >/dev/null 2>&1; then
echo -e "${RED}Error: '${YELLOW}$JSON_FILE${RED}' is not valid JSON.${NC}"
exit 1
fi
# Length checks for title (max 80) and abstract (max 2500) — only for fields that are present
LENGTH_CHECK_FAILED=0
LENGTH_CHECK_HEADER_SHOWN=0
check_length() {
local field="$1"
local max="$2"
local text len
text=$(jq -r ".body.$field // empty" "$JSON_FILE")
if [ -z "$text" ]; then
return
fi
if [ "$LENGTH_CHECK_HEADER_SHOWN" -eq 0 ]; then
echo -e " "
echo -e "${CYAN}Checking field length limits...${NC}"
LENGTH_CHECK_HEADER_SHOWN=1
fi
len=${#text}
if [ "$len" -gt "$max" ]; then
echo -e "${RED}Error: '${YELLOW}$field${RED}' is ${YELLOW}$len${RED} chars, exceeds max of ${YELLOW}$max${RED}.${NC}" >&2
LENGTH_CHECK_FAILED=1
else
echo -e "${WHITE}'$field' length: ${YELLOW}$len${WHITE} / $max${NC}"
fi
}
check_length title 80
check_length abstract 2500
if [ "$LENGTH_CHECK_FAILED" -ne 0 ]; then
exit 1
fi
# Basic spell check on key data fields (requires 'aspell' installed)
if [ "$check_spelling" = "true" ]; then
echo -e " "
echo -e "${CYAN}Applying spell check...${NC}"
# Fetch the upstream Intersect dictionary so users don't need a local copy
echo -e "${WHITE}Fetching Cardano aspell dictionary from ${YELLOW}$CARDANO_ASPELL_DICT_URL${NC}"
TMP_DICT_FILE=$(mktemp /tmp/cardano-aspell-dict.XXXXXX)
if ! curl --silent --show-error --fail --location --max-time 10 \
-o "$TMP_DICT_FILE" "$CARDANO_ASPELL_DICT_URL"; then
echo -e "${RED}Error: Failed to download aspell dictionary from ${YELLOW}$CARDANO_ASPELL_DICT_URL${NC}." >&2
echo -e "${YELLOW}Pass ${GREEN}--no-spell-check${YELLOW} to skip the spell check, or retry when online.${NC}" >&2
exit 1
fi
PERSONAL_DICT_ARG="--personal=$TMP_DICT_FILE"
# This hardcoded for CIP108
# todo fix for other schemas
SPELL_OUTPUT=""
for field in title abstract motivation rationale; do
text=$(jq -r ".body.$field // empty" "$JSON_FILE")
if [ -n "$text" ]; then
while IFS= read -r word; do
[ -n "$word" ] && SPELL_OUTPUT+=" ${BLUE}'$field': ${YELLOW}$word${NC}"$'\n'
done < <(echo "$text" | aspell list $PERSONAL_DICT_ARG | sort -u)
fi
done
if [ -n "$SPELL_OUTPUT" ]; then
echo -e "${YELLOW}Possible misspellings:${NC}"
printf '%b' "$SPELL_OUTPUT"
else
echo -e "${GREEN}No misspellings found.${NC}"
fi
fi
# URI reachability check — every URI (structured + markdown-embedded in prose fields)
# is HEAD-checked (with GET fallback). Duplicates are intentionally re-checked.
# Skip with --no-check-links. IPFS gateway from $IPFS_GATEWAY_URI or https://ipfs.io.
URI_CHECK_FAILED=0
if [ "$check_links" = "true" ]; then
echo -e " "
echo -e "${CYAN}Checking URI reachability...${NC}"
IPFS_GATEWAY="${IPFS_GATEWAY_URI:-https://ipfs.io}"
IPFS_GATEWAY="${IPFS_GATEWAY%/}"
uris=()
# 1. Structured URIs anywhere under .body with a key named uri/url (case-insensitive)
while IFS= read -r u; do
[ -n "$u" ] && uris+=("$u")
done < <(jq -r '
.body
| [.. | objects | to_entries[]
| select((.key | ascii_downcase) == "uri" or (.key | ascii_downcase) == "url")
| .value | select(type == "string")]
| .[]
' "$JSON_FILE")
# 2. Markdown-embedded / bare URLs in prose fields
for field in title abstract motivation rationale; do
text=$(jq -r ".body.$field // empty" "$JSON_FILE")
if [ -n "$text" ]; then
while IFS= read -r u; do
[ -n "$u" ] && uris+=("$u")
done < <(printf '%s' "$text" | perl -0777 -ne '
while (/\[[^\]]*\]\((https?:\/\/[^)\s]+|ipfs:\/\/[^)\s]+)\)/g) { print "$1\n" }
while (/<(https?:\/\/[^>\s]+|ipfs:\/\/[^>\s]+)>/g) { print "$1\n" }
while (/(?<![\w\/:])(https?:\/\/[^\s)\]<>"'"'"'\\]+)/g) { print "$1\n" }
while (/(?<![\w\/:])(ipfs:\/\/[A-Za-z0-9\/._\-]+)/g) { print "$1\n" }
')
fi
done
total_count=${#uris[@]}
failed_count=0
if [ "$total_count" -eq 0 ]; then
echo -e "${YELLOW}No URIs found to check.${NC}"
else
for raw_uri in "${uris[@]}"; do
# Trim trailing punctuation that regex may have grabbed
uri="$raw_uri"
while :; do
case "$uri" in
*.|*,|*\;|*\!|*\?|*\)|*\]|*\>|*\"|*\'|*\`) uri="${uri%?}" ;;
*) break ;;
esac
done
# Skip non-HTTP(S)/IPFS and fragment-only
case "$uri" in
mailto:*|tel:*) continue ;;
\#*) continue ;;
esac
if [[ ! "$uri" =~ ^(https?|ipfs):// ]]; then
continue
fi
# Normalize ipfs:// to configured gateway
check_url="$uri"
if [[ "$uri" == ipfs://* ]]; then
check_url="$IPFS_GATEWAY/ipfs/${uri#ipfs://}"
fi
http_code=$(curl --silent --location \
--max-time 10 --retry 1 --retry-delay 1 \
--head -o /dev/null -w '%{http_code}' "$check_url" 2>/dev/null || true)
[ -z "$http_code" ] && http_code="000"
if [ "$http_code" = "405" ] || [ "$http_code" = "000" ]; then
http_code=$(curl --silent --location \
--max-time 10 --retry 1 --retry-delay 1 \
--request GET --range 0-0 \
-o /dev/null -w '%{http_code}' "$check_url" 2>/dev/null || true)
[ -z "$http_code" ] && http_code="000"
fi
if [[ "$http_code" =~ ^[23][0-9][0-9]$ ]]; then
if [ "$uri" != "$check_url" ]; then
echo -e "${GREEN}[OK] ${NC} $uri ($http_code via $check_url)"
else
echo -e "${GREEN}[OK] ${NC} $uri ($http_code)"
fi
else
failed_count=$((failed_count + 1))
if [ "$uri" != "$check_url" ]; then
echo -e "${RED}[FAIL]${NC} $uri ($http_code via $check_url)" >&2
else
echo -e "${RED}[FAIL]${NC} $uri ($http_code)" >&2
fi
fi
done
echo -e " "
if [ "$failed_count" -gt 0 ]; then
echo -e "${RED}$failed_count of $total_count URIs unreachable.${NC}" >&2
echo -e "${YELLOW}Re-run with --no-check-links to skip this check (e.g. if offline).${NC}" >&2
URI_CHECK_FAILED=1
else
echo -e "${GREEN}All $total_count URIs reachable.${NC}"
fi
fi
fi
echo -e " "
echo -e "${CYAN}Applying schema check(s)...${NC}"
# Create a temporary directory for schema(s)
mkdir -p "$TMP_SCHEMAS_DIR"
# Download the schemas as needed
if [ "$use_cip_100" = "true" ]; then
echo -e "${WHITE}Downloading CIP-100 Governance Metadata schema...${NC}"
TEMP_CIP_100_SCHEMA="$TMP_SCHEMAS_DIR/cip-100-schema.json"
curl -sSfSL "$CIP_100_SCHEMA" -o "$TEMP_CIP_100_SCHEMA"
fi
if [ "$use_cip_108" = "true" ]; then
echo -e "${WHITE}Downloading CIP-108 Governance Actions schema...${NC}"
TEMP_CIP_108_SCHEMA="$TMP_SCHEMAS_DIR/cip-108-schema.json"
curl -sSfSL "$CIP_108_SCHEMA" -o "$TEMP_CIP_108_SCHEMA"
fi
if [ "$use_cip_119" = "true" ]; then
echo -e "${WHITE}Downloading CIP-119 DRep schema...${NC}"
TEMP_CIP_119_SCHEMA="$TMP_SCHEMAS_DIR/cip-119-schema.json"
curl -sSfSL "$CIP_119_SCHEMA" -o "$TEMP_CIP_119_SCHEMA"
fi
if [ "$use_cip_136" = "true" ]; then
echo -e "${WHITE}Downloading CIP-136 Constitutional Committee Vote schema...${NC}"
TEMP_CIP_136_SCHEMA="$TMP_SCHEMAS_DIR/cip-136-schema.json"
curl -sSfSL "$CIP_136_SCHEMA" -o "$TEMP_CIP_136_SCHEMA"
fi
if [ "$use_cip_169" = "true" ]; then
echo -e "${WHITE}Downloading CIP-169 Governance Metadata Extension schema...${NC}"
TEMP_CIP_169_SCHEMA="$TMP_SCHEMAS_DIR/cip-169-schema.json"
curl -sSfSL "$CIP_169_SCHEMA" -o "$TEMP_CIP_169_SCHEMA"
fi
# Determine which Intersect schema to use based on governanceActionType property
if [ "$use_intersect_schema" = "true" ]; then
governance_action_type=$(jq -r '.body.onChain.governanceActionType' "$JSON_FILE")
if [ "$governance_action_type" = "info" ]; then
echo -e "${WHITE}Downloading Intersect ${YELLOW}info${WHITE} schema...${NC}"
INTERSECT_SCHEMA_URL="$INTERSECT_INFO_SCHEMA"
elif [ "$governance_action_type" = "treasuryWithdrawals" ]; then
echo -e "${WHITE}Downloading Intersect ${YELLOW}treasuryWithdrawals${WHITE} schema...${NC}"
INTERSECT_SCHEMA_URL="$INTERSECT_TREASURY_SCHEMA"
elif [ "$governance_action_type" = "protocolParameterChanges" ]; then
echo -e "${WHITE}Downloading Intersect ${YELLOW}parameterChanges${WHITE} schema...${NC}"
INTERSECT_SCHEMA_URL="$INTERSECT_PPU_SCHEMA"
else
echo -e "${RED}Error: Unknown governanceActionType '${YELLOW}$governance_action_type${RED}' in '${YELLOW}$JSON_FILE${RED}'.${NC}"
exit 1
fi
TEMP_INT_SCHEMA="$TMP_SCHEMAS_DIR/intersect-schema.json"
curl -sSfSL "$INTERSECT_SCHEMA_URL" -o "$TEMP_INT_SCHEMA"
fi
if [ "$user_schema" = "true" ]; then
echo -e "${WHITE}Downloading schema from ${YELLOW}{$user_schema_url}${WHITE}...${NC}"
TEMP_USER_SCHEMA="$TMP_SCHEMAS_DIR/user-schema.json"
curl -sSfSL "$user_schema_url" -o "$TEMP_USER_SCHEMA"
fi
# Validate the JSON file against the schemas
schemas=("$TMP_SCHEMAS_DIR"/*-schema.json)
# In the case where flags are used to not have any schemas download, exit with 1
if [ -z "$(ls -A $TMP_SCHEMAS_DIR)" ]; then
echo -e "${RED}Error: No schemas were downloaded.${NC}"
exit 1
fi
VALIDATION_FAILED=0
for schema in "${schemas[@]}"; do
echo -e " "
echo -e "${CYAN}Validating against schema: ${YELLOW}$schema${NC}"
if [ -f "$schema" ]; then
ajv validate -s "$schema" -d "$JSON_FILE" --all-errors --strict=false
if [ $? -ne 0 ]; then
VALIDATION_FAILED=1
fi
fi
done
# Structural integrity check beyond what ajv enforces.
# Skipped entirely when only --schema URL is in use
STRUCT_CHECK_FAILED=0
if [ "$use_cip_100" = "true" ] || [ "$use_cip_108" = "true" ] || \
[ "$use_cip_119" = "true" ] || [ "$use_cip_136" = "true" ] || \
[ "$use_cip_169" = "true" ] || [ "$use_intersect_schema" = "true" ]; then
echo -e " "
echo -e "${CYAN}Applying structural integrity checks...${NC}"
# Non-empty authors array. Downgraded to a warning under --draft so the
authors_count=$(jq -r '(.authors // []) | length' "$JSON_FILE")
if [ "$authors_count" -eq 0 ]; then
if [ "$is_draft" = "true" ]; then
echo -e "${YELLOW}[WARN]${NC} ${WHITE}authors${NC}: array is empty. Acceptable under ${GREEN}--draft${NC}; re-run without ${GREEN}--draft${NC} after signing."
else
echo -e "${RED}[FAIL]${NC} ${WHITE}authors${NC}: array is empty. Governance metadata must declare at least one author for the document to be attestable. ${GRAY}(Add a witness or pass ${GREEN}--draft${GRAY} for pre-signing validation.)${NC}" >&2
STRUCT_CHECK_FAILED=1
fi
else
echo -e "${GREEN}[OK]${NC} ${WHITE}authors${NC}: $authors_count entr$([ "$authors_count" -eq 1 ] && echo y || echo ies)"
fi
fi
# Final result
if [ "$VALIDATION_FAILED" -ne 0 ] || [ "$URI_CHECK_FAILED" -ne 0 ] || [ "$STRUCT_CHECK_FAILED" -ne 0 ]; then
echo -e " "
echo -e "${RED}One or more validation errors were found.${NC}"
exit 1
else
echo -e " "
echo -e "${GREEN}No validation errors found.${NC}"
exit 0
fi