All files / src/cli-validator/utils validator.js

87.84% Statements 65/74
88.1% Branches 37/42
75% Functions 3/4
87.84% Lines 65/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 16728x 28x     28x       28x       28x     28x       28x   28x   28x                   28x           89x 89x 89x 89x                         89x 89x     89x       89x 89x 52x 52x   89x 55x 55x   89x 1x 1x   89x 1x 1x       89x 5x 5x 5x       5x 5x 5x           89x 780x 780x 17x 17x   780x 17x 17x   780x       780x           89x 1157x 1157x 74x       74x   1157x 164x       164x   1157x 1x       1x   1157x 1x       1x           89x 89x   89x                   89x    
const merge = require('deepmerge');
const getVersion = require('./getOpenApiVersion');
 
// import the validators
const semanticValidators2 = require('require-all')(
  __dirname + '/../../plugins/validation/swagger2/semantic-validators'
);
 
const semanticValidators3 = require('require-all')(
  __dirname + '/../../plugins/validation/oas3/semantic-validators'
);
 
const structuralValidator = require(__dirname +
  '/../../plugins/validation/2and3/structural-validation/validator');
 
const sharedSemanticValidators = require('require-all')(
  __dirname + '/../../plugins/validation/2and3/semantic-validators'
);
 
const circularRefsValidator = require('./circular-references-ibm');
 
const spectralValidator = require('../../spectral/utils/spectral-validator');
 
const validators = {
  '2': {
    semanticValidators: semanticValidators2
  },
  '3': {
    semanticValidators: semanticValidators3
  }
};
 
// this function runs the validators on the swagger object
module.exports = function validateSwagger(
  allSpecs,
  config,
  spectralResults,
  debug
) {
  const version = getVersion(allSpecs.jsSpec);
  allSpecs.isOAS3 = version === '3';
  const { semanticValidators } = validators[version];
  const validationResults = {
    errors: {},
    warnings: {},
    infos: {},
    hints: {},
    error: false,
    warning: false,
    info: false,
    hint: false
  };
 
  // use version specific and shared validations
  // they need to be at the top level of the config object
  const configSpecToUse = allSpecs.isOAS3 ? 'oas3' : 'swagger2';
  config = merge(config.shared, config[configSpecToUse]);
 
  // merge the spectral results
  const parsedSpectralResults = spectralValidator.parseResults(
    spectralResults,
    debug
  );
  const key = 'spectral';
  if (parsedSpectralResults.errors.length) {
    validationResults.errors[key] = [...parsedSpectralResults.errors];
    validationResults.error = true;
  }
  if (parsedSpectralResults.warnings.length) {
    validationResults.warnings[key] = [...parsedSpectralResults.warnings];
    validationResults.warning = true;
  }
  if (parsedSpectralResults.infos.length) {
    validationResults.infos[key] = [...parsedSpectralResults.infos];
    validationResults.info = true;
  }
  if (parsedSpectralResults.hints.length) {
    validationResults.hints[key] = [...parsedSpectralResults.hints];
    validationResults.hint = true;
  }
 
  // run circular reference validator
  if (allSpecs.circular) {
    const problem = circularRefsValidator.validate(allSpecs, config);
    const key = 'circular-references-ibm';
    Iif (problem.errors.length) {
      validationResults.errors[key] = [...problem.errors];
      validationResults.error = true;
    }
    Eif (problem.warnings.length) {
      validationResults.warnings[key] = [...problem.warnings];
      validationResults.warning = true;
    }
  }
 
  // run semantic validators
 
  Object.keys(semanticValidators).forEach(key => {
    const problem = semanticValidators[key].validate(allSpecs, config);
    if (problem.errors.length) {
      validationResults.errors[key] = [...problem.errors];
      validationResults.error = true;
    }
    if (problem.warnings.length) {
      validationResults.warnings[key] = [...problem.warnings];
      validationResults.warning = true;
    }
    Iif (problem.infos.length) {
      validationResults.infos[key] = [...problem.infos];
      validationResults.info = true;
    }
    Iif (problem.hints.length) {
      validationResults.hints[key] = [...problem.hints];
      validationResults.hint = true;
    }
  });
 
  Object.keys(sharedSemanticValidators).forEach(key => {
    const problem = sharedSemanticValidators[key].validate(allSpecs, config);
    if (problem.errors.length) {
      validationResults.errors[key] = [].concat(
        validationResults.errors[key] || [],
        problem.errors
      );
      validationResults.error = true;
    }
    if (problem.warnings.length) {
      validationResults.warnings[key] = [].concat(
        validationResults.warnings[key] || [],
        problem.warnings
      );
      validationResults.warning = true;
    }
    if (problem.infos.length) {
      validationResults.infos[key] = [].concat(
        validationResults.infos[key] || [],
        problem.infos
      );
      validationResults.info = true;
    }
    if (problem.hints.length) {
      validationResults.hints[key] = [].concat(
        validationResults.hints[key] || [],
        problem.hints
      );
      validationResults.hint = true;
    }
  });
 
  // run structural validator
  // all structural problems are errors
  const structuralResults = structuralValidator.validate(allSpecs);
  const structuralKeys = Object.keys(structuralResults);
 
  Iif (structuralKeys.length) {
    validationResults.error = true;
    validationResults.errors['structural-validator'] = structuralKeys.map(
      key => ({
        message: `Schema error: ${structuralResults[key].message}`,
        path: structuralResults[key].path
      })
    );
  }
 
  return validationResults;
};