forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-openapi
executable file
·167 lines (154 loc) · 5.81 KB
/
check-openapi
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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const Diff = require("diff");
const ExampleValidator = require("openapi-examples-validator");
const Prettier = require("prettier");
const SwaggerParser = require("swagger-parser");
const {Composer, CST, LineCounter, Parser, Scalar, YAMLMap, YAMLSeq, visit} = require("yaml");
const {hideBin} = require("yargs/helpers");
const yargs = require("yargs/yargs");
const argv = yargs(hideBin(process.argv))
.option("fix", {
type: "boolean",
description: "Automatically fix some problems",
})
.parse();
async function checkFile(file) {
const yaml = await fs.promises.readFile(file, "utf8");
const lineCounter = new LineCounter();
const tokens = [...new Parser(lineCounter.addNewLine).parse(yaml)];
const docs = [...new Composer().compose(tokens)];
if (docs.length !== 1) {
return;
}
const [doc] = docs;
if (doc.errors.length > 0) {
for (const error of doc.errors) {
console.error("%s: %s", file, error.message);
}
process.exitCode = 1;
return;
}
const root = doc.contents;
if (!(root instanceof YAMLMap && root.has("openapi"))) {
return;
}
let ok = true;
const reformats = new Map();
const promises = [];
visit(doc, {
Map(_key, node) {
if (node.has("$ref") && node.items.length !== 1) {
const {line, col} = lineCounter.linePos(node.range[0]);
console.error("%s:%d:%d: Siblings of $ref have no effect", file, line, col);
ok = false;
}
},
Pair(_key, node) {
if (
node.key instanceof Scalar &&
node.key.value === "allOf" &&
node.value instanceof YAMLSeq &&
node.value.items.filter(
(subschema) => !(subschema instanceof YAMLMap && subschema.has("$ref")),
).length > 1
) {
const {line, col} = lineCounter.linePos(node.value.range[0]);
console.error("%s:%d:%d: Too many inline allOf subschemas", file, line, col);
ok = false;
}
if (
node.key instanceof Scalar &&
node.key.value === "description" &&
node.value instanceof Scalar &&
typeof node.value.value === "string"
) {
promises.push(
(async () => {
let formatted = await Prettier.format(node.value.value, {
parser: "markdown",
});
if (
![Scalar.BLOCK_FOLDED, Scalar.BLOCK_LITERAL].includes(node.value.type)
) {
formatted = formatted.replace(/\n$/, "");
}
if (formatted !== node.value.value) {
if (argv.fix) {
reformats.set(node.value.range[0], {
value: formatted,
context: {afterKey: true},
});
} else {
ok = false;
const {line, col} = lineCounter.linePos(node.value.range[0]);
console.error(
"%s:%d:%d: Format description with Prettier:",
file,
line,
col,
);
let diff = "";
for (const part of Diff.diffLines(node.value.value, formatted)) {
const prefix = part.added
? "\u001B[32m+"
: part.removed
? "\u001B[31m-"
: "\u001B[34m ";
diff += prefix;
diff += part.value
.replace(/\n$/, "")
.replaceAll("\n", "\n" + prefix);
diff += "\n";
}
diff += "\u001B[0m";
console.error(diff);
}
}
})(),
);
}
},
});
await Promise.all(promises);
if (!ok) {
process.exitCode = 1;
}
if (reformats.size > 0) {
console.log("%s: Fixing problems", file);
for (const token of tokens) {
CST.visit(token, ({value}) => {
if (CST.isScalar(value) && reformats.has(value.offset)) {
const reformat = reformats.get(value.offset);
CST.setScalarValue(value, reformat.value, reformat.context);
}
});
}
await fs.promises.writeFile(file, tokens.map((token) => CST.stringify(token)).join(""));
}
try {
await SwaggerParser.validate(file);
} catch (error) {
if (!(error instanceof SyntaxError)) {
throw error;
}
console.error("%s: %s", file, error.message);
process.exitCode = 1;
}
const res = await ExampleValidator.validateFile(file);
if (!res.valid) {
for (const error of res.errors) {
console.error(error);
}
process.exitCode = 1;
}
}
(async () => {
for (const file of argv._) {
await checkFile(file);
}
})().catch((error) => {
console.error(error);
process.exit(1);
});