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
|
import fs from 'fs-extra';
import upath from 'upath';
import { Json } from '../lib/util/schema-utils';
import { capitalize } from '../tools/docs/utils';
import * as Schemas from '../tools/schemas/schema';
describe('validate-schemas', () => {
it('validate json files in lib/data against their schemas', async () => {
const dataFileDir = 'lib/data';
const schemaDir = 'tools/schemas';
const schemasAndJsonFiles: {
schemaName: keyof typeof Schemas;
dataFileName: string;
}[] = [];
const schemaFiles = (await fs.readdir(schemaDir)).filter(
(file) => upath.extname(file) === '.json',
);
for (const schemaFile of schemaFiles) {
const correspondingDataFileName = schemaFile.replace('-schema', '');
const schemaName = `${schemaFile
.replace('.json', '')
.split('-')
.map(capitalize)
.join('')}` as keyof typeof Schemas;
schemasAndJsonFiles.push({
schemaName,
dataFileName: correspondingDataFileName,
});
}
const settledPromises = await Promise.allSettled(
schemasAndJsonFiles.map(async ({ schemaName, dataFileName }) => {
const data = Json.parse(
await fs.readFile(upath.join(dataFileDir, dataFileName), 'utf8'),
);
// validate json data against schema: using parse here instead of safeParse so we throw
// this leads to a better error message when the assertion fails
// eslint-disable-next-line import/namespace
Schemas[schemaName].parse(data);
}),
);
for (let i = 0; i < settledPromises.length; i++) {
const { schemaName, dataFileName } = schemasAndJsonFiles[i];
const res = {
schemaName,
dataFileName,
settledPromise: { reason: undefined, ...settledPromises[i] },
};
expect(res).toMatchObject({
schemaName,
dataFileName,
settledPromise: {
status: 'fulfilled',
reason: undefined,
},
});
}
});
});
|