How to use the oas-validator.validateSync function in oas-validator

To help you get started, we’ve selected a few oas-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IBM / openapi-to-graphql / packages / openapi-to-graphql / lib / oas_3_tools.js View on Github external
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`);
        }
    });
}
github IBM / openapi-to-graphql / packages / openapi-to-graphql / src / oas_3_tools.ts View on Github external
) {
    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`)
  }
}
github openapi-contrib / openapi3-generator / lib / bundler.js View on Github external
} 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));
}
github Redocly / swagger-repo / lib / index.js View on Github external
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, {});
github Mermade / openapi-webconverter / api.js View on Github external
}).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');
                }
github Mermade / oas-kit / packages / swagger2openapi / boast.js View on Github external
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);
github Mermade / openapi-webconverter / api.js View on Github external
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);

        });
    }
github Mermade / openapi-codegen / adaptor.js View on Github external
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);
    }
github atlassian / koa-oas3 / src / index.ts View on Github external
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,
  };
}

oas-validator

Parser/validator for OpenAPI 3.x definitions

BSD-3-Clause
Latest version published 3 years ago

Package Health Score

71 / 100
Full package analysis