Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let resultStr2 = yaml.stringify(result); // FIXME dropped 'noRefs:true' here
should(resultStr).not.be.exactly('{}','Result should not be empty');
should(resultStr).equal(resultStr2,'Result should have no object identity ref_s');
}
catch (ex) {
if (options.verbose>1) {
fs.writeFileSync('./debug.yaml',resultStr,'utf8');
console.warn('Result dumped to debug.yaml fixed.yaml');
let fix = reref(result);
fs.writeFileSync('./fixed.yaml',yaml.stringify(fix),'utf8');
}
should.fail(false,true,'Result cannot be represented safely in YAML');
}
}
validator.validate(result, options)
.then(function(options){
finalise(null,options);
})
.catch(function(ex){
finalise(ex,options);
});
}
catch (ex) {
console.warn(common.colour.normal + options.file);
console.warn(common.colour.red + (options.context.length ? options.context.pop() : 'No context')+ '\n' + ex.message);
if (ex.stack && ex.name !== 'AssertionError' && ex.name !== 'CLIError') {
console.warn(ex.stack);
}
options.valid = !options.expectFailure;
finalise(ex, options);
}
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('You must have an open editor window to validate an OpenAPI document');
return; // No open text editor
}
if (resolve && editor.document.isUntitled) {
vscode.window.showWarningMessage('Document must be saved in order to resolve correctly');
return; // No open text editor
}
let text = editor.document.getText();
try {
let options = { lint: lint, resolve: resolve, fatal: true, source: editor.document.fileName };
let obj = yaml.parse(text);
validator.validate(obj, options)
.then(function(){
vscode.window.showInformationMessage('Your OpenAPI document is '+(lint ? 'excellent!' : 'valid.'));
})
.catch(function(ex){
dc.delete(editor.document.uri);
const diagnostics = [];
let range; // TODO
diagnostics.push(new vscode.Diagnostic(range, ex.message, vscode.DiagnosticSeverity.Error));
for (let warning of options.warnings||[]) {
diagnostics.push(new vscode.Diagnostic(range, warning.message + ' ' + warning.ruleName, vscode.DiagnosticSeverity.Warning));
}
dc.set(editor.document.uri, diagnostics);
});
}
catch (ex) {
vscode.window.showErrorMessage('Could not parse OpenAPI document as JSON or YAML!');
options.format = 'asyncapi_1';
api.asyncapi = api.version;
delete api.version;
recurse(api,{},function(obj,key,state){
if (key === 'asyncapi_servers_variables') {
obj.variables = obj.asyncapi_servers_variables;
delete obj.asyncapi_servers_variables;
}
});
}
}
if (options.format === 'openapi_3') {
var options = {laxRefs:true,resolve:true,source:options.source,convert:options.convert};
try {
openapi3.validate(api, options, function(err, opts) {
if (err) {
return callback(err, err.options||opts||options);
}
options.converted = options.openapi||api;
options.message = 'Valid openapi 3.0.x';
options.context = [api.info.version + ' ' +
(api.servers && api.servers.length ? api.servers[0].url : 'Relative')];
callback(null, options);
});
}
catch (ex) {
var context = options.context.pop();
options.context = [context];
callback(ex, options);
}
}