2019-08-07 23:18:05 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2020-03-28 04:42:06 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const jsyaml = require('js-yaml');
|
2020-02-06 03:23:22 +01:00
|
|
|
const SwaggerParser = require('swagger-parser');
|
2020-05-11 16:26:33 +02:00
|
|
|
const ExampleValidator = require('openapi-examples-validator');
|
2017-06-02 23:12:26 +02:00
|
|
|
|
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 {
|
|
|
|
if (
|
|
|
|
jsyaml.safeLoad(await fs.promises.readFile(file, 'utf8'), {
|
|
|
|
filename: file,
|
|
|
|
}).openapi !== undefined
|
|
|
|
) {
|
|
|
|
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);
|
|
|
|
});
|