-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_evidence_schema.mjs
More file actions
59 lines (52 loc) · 1.31 KB
/
Copy pathvalidate_evidence_schema.mjs
File metadata and controls
59 lines (52 loc) · 1.31 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
#!/usr/bin/env node
import fs from "node:fs";
import process from "node:process";
import Ajv2020 from "ajv/dist/2020.js";
import addFormats from "ajv-formats";
const [schemaPath, ...documentPaths] = process.argv.slice(2);
if (!schemaPath || documentPaths.length === 0) {
console.error(
"usage: validate_evidence_schema.mjs <schema.json> <document.json> [...]",
);
process.exit(2);
}
let schema;
try {
schema = JSON.parse(fs.readFileSync(schemaPath, "utf8"));
} catch (error) {
console.error(`schema:${schemaPath}: ${error.message}`);
process.exit(2);
}
const ajv = new Ajv2020({
allErrors: true,
strict: true,
validateFormats: true,
});
addFormats(ajv);
let validate;
try {
validate = ajv.compile(schema);
} catch (error) {
console.error(`schema:${schemaPath}: ${error.message}`);
process.exit(2);
}
let failed = false;
for (const documentPath of documentPaths) {
let document;
try {
document = JSON.parse(fs.readFileSync(documentPath, "utf8"));
} catch (error) {
console.error(`${documentPath}: ${error.message}`);
failed = true;
continue;
}
if (!validate(document)) {
failed = true;
for (const error of validate.errors ?? []) {
console.error(
`${documentPath}:${error.instancePath || "/"} ${error.message}`,
);
}
}
}
process.exit(failed ? 1 : 0);