Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, {});
}
cb(!valid);