2019-08-07 23:18:05 +02:00
|
|
|
#!/usr/bin/env node
|
2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
2019-08-07 23:18:05 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const fs = require("fs");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const jsyaml = require("js-yaml");
|
|
|
|
const ExampleValidator = require("openapi-examples-validator");
|
2020-07-24 06:02:07 +02:00
|
|
|
const SwaggerParser = require("swagger-parser");
|
2017-06-02 23:12:26 +02:00
|
|
|
|
2020-08-11 22:57:50 +02:00
|
|
|
function checkRefSiblings(file, path, data) {
|
|
|
|
let ok = true;
|
|
|
|
if (typeof data === "object" && data !== null) {
|
2020-10-01 03:52:34 +02:00
|
|
|
if (
|
|
|
|
"allOf" in data &&
|
|
|
|
Object.values(data.allOf).filter((subschema) => !("$ref" in subschema)).length !== 1
|
|
|
|
) {
|
|
|
|
console.error(
|
|
|
|
`${file}: Too many inline allOf subschemas at ${JSON.stringify(
|
|
|
|
path,
|
|
|
|
)}: ${JSON.stringify(data, undefined, 2)}`,
|
|
|
|
);
|
|
|
|
ok = false;
|
|
|
|
}
|
2020-08-11 22:57:50 +02:00
|
|
|
if ("$ref" in data && Object.entries(data).length !== 1) {
|
|
|
|
console.error(
|
|
|
|
`${file}: Siblings of $ref have no effect at ${JSON.stringify(
|
|
|
|
path,
|
|
|
|
)}: ${JSON.stringify(data, undefined, 2)}`,
|
|
|
|
);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
for (const [key, child] of Array.isArray(data) ? data.entries() : Object.entries(data)) {
|
|
|
|
if (!checkRefSiblings(file, [...path, key], child)) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-03-28 04:42:06 +01:00
|
|
|
(async () => {
|
|
|
|
// Iterate through the changed files, passed in the arguments.
|
|
|
|
// The two first arguments are the call to the Node interpreter and this
|
|
|
|
// script, hence the starting point at 2.
|
|
|
|
for (const file of process.argv.slice(2)) {
|
2020-05-07 00:04:22 +02:00
|
|
|
try {
|
2021-01-22 22:13:38 +01:00
|
|
|
const data = jsyaml.load(await fs.promises.readFile(file, "utf8"), {
|
2020-08-11 22:57:50 +02:00
|
|
|
filename: file,
|
|
|
|
});
|
|
|
|
if (data.openapi !== undefined) {
|
|
|
|
if (!checkRefSiblings(file, [], data)) {
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
2020-05-07 00:04:22 +02:00
|
|
|
await SwaggerParser.validate(file);
|
2020-05-11 16:26:33 +02:00
|
|
|
const res = await ExampleValidator.validateFile(file);
|
|
|
|
if (!res.valid) {
|
|
|
|
for (const error of res.errors) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
2020-05-07 00:04:22 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof jsyaml.YAMLException) {
|
|
|
|
console.error(error.message);
|
|
|
|
} else if (error instanceof SyntaxError) {
|
|
|
|
console.error(`${file}: ${error.message}`);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
process.exitCode = 1;
|
2020-03-28 04:42:06 +01:00
|
|
|
}
|
2017-06-02 23:12:26 +02:00
|
|
|
}
|
2020-05-07 00:04:22 +02:00
|
|
|
})().catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|