Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return __awaiter(this, void 0, void 0, function* () {
// CASE: translate
if (typeof spec.swagger === 'string' &&
spec.swagger === '2.0') {
preprocessingLog(`Received OpenAPI Specification 2.0 - going to translate...`);
const result = yield Swagger2OpenAPI.convertObj(spec, {});
return result.openapi;
// CASE: validate
}
else if (typeof spec.openapi === 'string' &&
/^3/.test(spec.openapi)) {
preprocessingLog(`Received OpenAPI Specification 3.0.x - going to validate...`);
const valid = OASValidator.validateSync(spec, {});
if (!valid) {
throw new Error(`Validation of OpenAPI Specification failed.`);
}
preprocessingLog(`OpenAPI Specification is validated`);
return spec;
}
else {
throw new Error(`Invalid specification provided`);
}
});
}
) {
preprocessingLog(
`Received OpenAPI Specification 2.0 - going to translate...`
)
const result: { openapi: Oas3 } = await Swagger2OpenAPI.convertObj(spec, {})
return result.openapi as Oas3
// CASE: validate
} else if (
typeof (spec as Oas3).openapi === 'string' &&
/^3/.test((spec as Oas3).openapi)
) {
preprocessingLog(
`Received OpenAPI Specification 3.0.x - going to validate...`
)
const valid = OASValidator.validateSync(spec, {})
if (!valid) {
throw new Error(`Validation of OpenAPI Specification failed.`)
}
preprocessingLog(`OpenAPI Specification is validated`)
return spec as Oas3
} else {
throw new Error(`Invalid specification provided`)
}
}
} catch (e) {
console.error('Can not dereference the JSON obtained from the content of the Swagger specification file');
console.error(e);
return;
}
try {
bundledJSON = await bundle(dereferencedJSON);
} catch (e) {
console.error('Can not bundle the JSON obtained from the content of the Swagger specification file');
console.error(e);
return;
}
try {
validator.validateSync(bundledJSON, {});
} catch (e) {
console.error('Invalid OpenAPI file');
console.error(e);
return;
}
return JSON.parse(JSON.stringify(bundledJSON));
}
exports.validate = function(spec, options = {}, cb) {
if (spec.openapi) {
const validator = require('oas-validator');
const validateOptions = { prettify: false, lint: false, validateSchema: 'first' };
let valid = false;
const paths = options.mainFile ? { mainFile: options.mainFile } : calcPaths(options.basedir);
try {
valid = validator.validateSync(spec, validateOptions);
} catch (e) {
if (e instanceof validator.JSONSchemaError) {
console.error(chalk.red('Failed OpenAPI3 schema validation:\n'));
const errors = JSON.parse(e.message.replace(/^.*\[/, '['));
betterErrors(errors, paths);
} else {
console.error(chalk.red(`Lint error:\n`));
e.keyword = '__lint';
e.dataPath = validateOptions.context.pop() || '';
if (e.dataPath.startsWith('#')) {
e.dataPath = e.dataPath.substring(1);
}
console.log(e.dataPath);
betterErrors([e], paths);
}
return cb(true, {});
}).then(async function(body) {
var obj = getObj(body,payload);
var options = { source: req.query.url, resolve:true };
try {
result.status = await validator.validateSync(obj,options);
result.openapi = options.openapi.openapi || false;
}
catch(ex) {
result.message = ex.message||'No message';
console.warn(ex);
result.context = options.context.pop();
result.openapi = obj.openapi || false;
}
if (badge) {
status.badges++;
if (result.status) {
res.redirect('https://img.shields.io/badge/OpenAPI3-Valid-brightgreen.svg');
}
else {
res.redirect('https://img.shields.io/badge/OpenAPI3-Invalid-red.svg');
}
argv.prettify = true;
}
if (argv.internal) {
argv.resolveInternal = true;
}
let options = {};
let result = false;
let jsonOutput = {};
try {
if (argv.source.startsWith('http')) {
options = await swagger2openapi.convertUrl(argv.source,argv);
}
else {
options = await swagger2openapi.convertFile(argv.source,argv);
}
result = await validator.validateSync(options.openapi,options);
}
catch (ex) {
let path;
if (options.context) {
path = options.context.pop();
}
if (options.json) {
jsonOutput.error = ex.message;
if (options.verbose > 1) jsonOutput.stacktrace = ex.stack;
if (path) {
jsonOutput.path = path;
}
}
else {
console.warn(ex.message);
if (options.verbose > 1) console.warn(ex.stack);
converter.convert(obj,options,async function(err,options){
if (err) {
result.message = err.message||'no message';
result.options = sanitise(result.options);
}
else {
result = options.openapi;
if (options.openapi && options.openapi.openapi && options.openapi.openapi.startsWith('3.')) payload.status = 200;
}
if (validate && !err) {
status.validations++;
try {
result = {};
result.status = await validator.validateSync(options.openapi,options);
if (result.status === true) payload.status = 200;
}
catch (ex) {
result.message = ex.message;
console.warn(ex);
if (options && options.context) {
result.context = options.context.pop();
}
}
}
finishConversion(res,result,payload);
});
}
if (defaults.verbose) console.log(message);
}
for (let w of result.warnings) {
let message = {};
message.level = 'Warning';
message.elementType = 'Path';
message.message = w.message;
message.elementId = w.path.join('/');
obj.messages.push(message);
if (defaults.verbose) console.log(message);
}
}
});
}
else try {
validator(api,vOptions);
message.level = 'Valid';
message.elementType = 'Context';
message.elementId = 'None';
message.message = 'No validation errors detected';
obj.messages.push(message);
if (defaults.verbose) console.log(message);
}
catch (ex) {
message.level = 'Error';
message.elementType = 'Context';
message.elementId = vOptions.context.pop();
message.message = ex.message;
obj.messages.push(message);
console.error(message);
}
function compileOas(config: Config) {
let openApiObject: any = config.spec || loadFromFile(config.file);
if (!oasValidator.validateSync(openApiObject, {})) {
throw new Error('Invalid Openapi document');
}
return {
compiled: new ChowChow(openApiObject),
doc: openApiObject,
};
}