-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-jsonapi-examples.js
51 lines (40 loc) · 1.31 KB
/
validate-jsonapi-examples.js
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
const glob = require("glob");
const path = require("path");
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const ajv = new Ajv({
strict: false,
});
// ensure uri-reference is available
addFormats(ajv);
// use draft-06 jsonschema standard
const draft6MetaSchema = require("ajv/dist/refs/json-schema-draft-06.json");
ajv.addMetaSchema(draft6MetaSchema);
const jsonapiJsonSchema = require("./spec/jsonapi-schema.json");
const validate = ajv.compile(jsonapiJsonSchema);
const validFiles = [];
const errorsByFilename = {};
const jsonapiResponseFiles = glob.sync("./spec/test-data/jsonapi/**/*.json");
jsonapiResponseFiles.forEach((file) => {
const data = require(path.resolve(file));
const validData = validate(data);
if (!validData) {
errorsByFilename[file] = validate.errors;
} else {
validFiles.push(file);
}
});
const invalidFilenames = Object.keys(errorsByFilename);
console.log(
`The test-data contains ${validFiles.length} valid JSON:API example file(s)`,
);
if (invalidFilenames.length > 0) {
console.error(
`The test-data contains ${invalidFilenames.length} invalid JSON:API example file(s)`,
);
invalidFilenames.forEach((invalidFile) => {
console.error(`\n\n=== ${invalidFile} ===`);
console.error(errorsByFilename[invalidFile]);
});
process.exit(1);
}