-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathvalidate.js
More file actions
182 lines (152 loc) · 6.09 KB
/
Copy pathvalidate.js
File metadata and controls
182 lines (152 loc) · 6.09 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
/**
* validate.js - Validates all well-known ABI spec files against schema.json
* and all label files in data/labels/ against label.schema.json.
*
* Usage:
* node validate.js
*
* Exit codes:
* 0 All specs and labels pass validation.
* 1 One or more specs or labels fail validation, or a file cannot be read/parsed.
*/
import { readFileSync, readdirSync } from "node:fs";
import { resolve, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import Ajv from "ajv";
const __dirname = dirname(fileURLToPath(import.meta.url));
// ── Spec validation ──────────────────────────────────────────────────────────
const SCHEMA_PATH = resolve(__dirname, "specs/well-known/schema.json");
const SPECS_DIR = resolve(__dirname, "specs/well-known");
// Files that are not contract specs and should be skipped.
const SPEC_SKIP = new Set(["schema.json", "index.json"]);
// ---------------------------------------------------------------------------
// Load spec schema
// ---------------------------------------------------------------------------
let specSchema;
try {
specSchema = JSON.parse(readFileSync(SCHEMA_PATH, "utf8"));
} catch (err) {
process.stderr.write(`[validate] Cannot read spec schema: ${SCHEMA_PATH}\n ${err.message}\n`);
process.exit(1);
}
const ajv = new Ajv({ allErrors: true });
const validateSpec = ajv.compile(specSchema);
// ---------------------------------------------------------------------------
// Discover spec files
// ---------------------------------------------------------------------------
let specFiles;
try {
specFiles = readdirSync(SPECS_DIR)
.filter((f) => f.endsWith(".json") && !SPEC_SKIP.has(f))
.sort();
} catch (err) {
process.stderr.write(`[validate] Cannot read specs directory: ${SPECS_DIR}\n ${err.message}\n`);
process.exit(1);
}
if (specFiles.length === 0) {
process.stderr.write(`[validate] No spec files found in ${SPECS_DIR}\n`);
process.exit(1);
}
// ---------------------------------------------------------------------------
// Validate each spec
// ---------------------------------------------------------------------------
let specPassed = 0;
let specFailed = 0;
for (const file of specFiles) {
const filePath = join(SPECS_DIR, file);
let data;
try {
data = JSON.parse(readFileSync(filePath, "utf8"));
} catch (err) {
process.stderr.write(`[validate/spec] FAIL ${file}\n Parse error: ${err.message}\n`);
specFailed++;
continue;
}
const valid = validateSpec(data);
if (valid) {
process.stdout.write(`[validate/spec] PASS ${file}\n`);
specPassed++;
} else {
process.stderr.write(`[validate/spec] FAIL ${file}\n`);
for (const error of validateSpec.errors) {
const field = error.instancePath || "(root)";
process.stderr.write(` ${field}: ${error.message}\n`);
}
specFailed++;
}
}
// ── Label validation ─────────────────────────────────────────────────────────
const LABEL_SCHEMA_PATH = resolve(__dirname, "schemas/label.schema.json");
const LABELS_DIR = resolve(__dirname, "../../data/labels");
// Files that are not label records and should be skipped.
const LABEL_SKIP = new Set(["index.json"]);
// ---------------------------------------------------------------------------
// Load label schema
// ---------------------------------------------------------------------------
let labelSchema;
try {
labelSchema = JSON.parse(readFileSync(LABEL_SCHEMA_PATH, "utf8"));
} catch (err) {
process.stderr.write(`[validate/label] Cannot read label schema: ${LABEL_SCHEMA_PATH}\n ${err.message}\n`);
process.exit(1);
}
const validateLabel = ajv.compile(labelSchema);
// ---------------------------------------------------------------------------
// Discover label files
// ---------------------------------------------------------------------------
let labelFiles;
try {
labelFiles = readdirSync(LABELS_DIR)
.filter((f) => f.endsWith(".json") && !LABEL_SKIP.has(f))
.sort();
} catch (err) {
process.stderr.write(`[validate/label] Cannot read labels directory: ${LABELS_DIR}\n ${err.message}\n`);
process.exit(1);
}
if (labelFiles.length === 0) {
process.stderr.write(`[validate/label] No label files found in ${LABELS_DIR}\n`);
process.exit(1);
}
// ---------------------------------------------------------------------------
// Validate each label
// ---------------------------------------------------------------------------
let labelPassed = 0;
let labelFailed = 0;
for (const file of labelFiles) {
const filePath = join(LABELS_DIR, file);
let data;
try {
data = JSON.parse(readFileSync(filePath, "utf8"));
} catch (err) {
process.stderr.write(`[validate/label] FAIL ${file}\n Parse error: ${err.message}\n`);
labelFailed++;
continue;
}
const valid = validateLabel(data);
if (valid) {
// Extra check: at least one verifiable source URL (CI requirement)
if (!Array.isArray(data.sources) || data.sources.length === 0) {
process.stderr.write(`[validate/label] FAIL ${file}\n sources: must have at least one verifiable source URL\n`);
labelFailed++;
continue;
}
process.stdout.write(`[validate/label] PASS ${file}\n`);
labelPassed++;
} else {
process.stderr.write(`[validate/label] FAIL ${file}\n`);
for (const error of validateLabel.errors) {
const field = error.instancePath || "(root)";
process.stderr.write(` ${field}: ${error.message}\n`);
}
labelFailed++;
}
}
// ---------------------------------------------------------------------------
// Summary
// ---------------------------------------------------------------------------
const totalPassed = specPassed + labelPassed;
const totalFailed = specFailed + labelFailed;
process.stdout.write(`\n[validate] ${specPassed}/${specFiles.length} specs passed, ${labelPassed}/${labelFiles.length} labels passed (${totalPassed} passed, ${totalFailed} failed overall)\n`);
if (totalFailed > 0) {
process.exit(1);
}