How to use the oas-validator.JSONSchemaError 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 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, {});
    }
    cb(!valid);

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