forked from Busch-Jaeger/node-free-at-home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate
executable file
·136 lines (117 loc) · 3.88 KB
/
validate
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
#!/usr/bin/env node
const Enforcer = require('openapi-enforcer');
const fs = require('fs');
const path = require('path');
const config = {
exitCode: 0,
schemaFiles: [],
addonMetaFiles: []
};
const componentOptions = {
exceptionSkipCodes: ['WSCH001'] // Non standard format "x" used for type "string". [WSCH001]
};
function exitWithError(error) {
console.error(error);
process.exit(1);
}
const help = `
Validates free@home openapi schema and addon metadata files.
Usage: validate <options> <dirs>
Options:
-e <code> exit with error code when validation failed
default: 0 when option is missing, 1 when open is used without <code>
--help show this text
<dirs> List of directories to check for YAML or free-at-home-metadata.json files to validate.
`;
let hasDirs = false;
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg === '-e') {
if (/\d+/.test(process.argv[i+1])) {
config.exitCode = parseInt(process.argv[i+1]);
i++;
} else {
config.exitCode = 1;
}
} else if (arg === '--help') {
console.log(help);
process.exit(0);
} else if (arg.startsWith('-')) {
exitWithError('invalid option', arg);
} else {
hasDirs = true;
if (fs.existsSync(arg)) {
for (const file of fs.readdirSync(arg)) {
if (file.endsWith(".yaml")) {
config.schemaFiles.push(path.join(process.cwd(), arg, file));
} else if (file === 'free-at-home-metadata.json') {
config.addonMetaFiles.push(path.join(process.cwd(), arg, file));
}
}
} else {
console.error(arg, 'does not exist');
}
}
}
if (!hasDirs) {
// check if we are running inside an addon dir
const meta = path.join(process.cwd(), 'free-at-home-metadata.json');
if (fs.existsSync(meta)) {
config.addonMetaFiles.push(meta);
} else {
const specs = path.join(process.cwd(), 'specs');
if (fs.existsSync(specs)) {
for (const file of fs.readdirSync(specs)) {
if (file.endsWith(".yaml")) {
config.schemaFiles.push(path.join(specs, file));
}
}
}
}
}
let errorsFound = false;
function validateMetadata(addonMetaFiles) {
Enforcer(path.join(__dirname, 'specs', 'addon.yaml'), { componentOptions })
.then(openapi => {
const metadataSchema = openapi.components.schemas.Metadata;
for (let addonMetaFile of addonMetaFiles) {
const metadata = require(addonMetaFile);
const err = metadataSchema.validate(metadata);
if (err) {
console.error('❌', addonMetaFile, 'is invalid');
console.error(err.toString());
errorsFound = true;
} else {
console.log('✅', addonMetaFile, 'is valid');
}
}
}).catch(err => {
console.error(err);
process.exit(1);
})
}
async function validateSchemas(schemaFiles) {
for (const schemaFile of schemaFiles) {
const [openapi, error, warning] = await Enforcer(schemaFile, { fullResult: true, componentOptions });
if (error !== undefined) {
console.error('❌', schemaFile, 'is invalid:');
console.error(error.toString());
}
if (warning !== undefined) {
console.warn(schemaFile, 'has warnings:');
console.warn(warning)
}
if (openapi !== undefined) {
console.log('✅', schemaFile, 'is valid');
}
}
}
if (config.addonMetaFiles.length > 0) {
validateMetadata(config.addonMetaFiles);
}
if (config.schemaFiles.length > 0) {
validateSchemas(config.schemaFiles);
}
if (errorsFound) {
process.exit(config.exitCode);
}