All files / src/lib index.js

90% Statements 18/20
50% Branches 1/2
100% Functions 1/1
90% Lines 18/20

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 4320x 20x 20x 20x 20x 20x 20x 20x   20x                   42x       42x 42x 42x         42x   42x 42x       42x     42x    
const chalk = require('chalk');
const config = require('../cli-validator/utils/processConfiguration');
const buildSwaggerObject = require('../cli-validator/utils/buildSwaggerObject');
const validator = require('../cli-validator/utils/validator');
const { formatResultsAsObject } = require('../cli-validator/utils/jsonResults');
const spectralValidator = require('../spectral/utils/spectral-validator');
const dedupFunction = require('../cli-validator/utils/noDeduplication');
const { Spectral } = require('@stoplight/spectral');
 
module.exports = async function(
  input,
  defaultMode = false,
  configFileOverride = null
) {
  // process the config file for the validations &
  // create an instance of spectral & load the spectral ruleset, either a user's
  // or the default ruleset
  let configObject;
  let spectralResults;
  const spectral = new Spectral({
    computeFingerprint: dedupFunction
  });
 
  try {
    configObject = await config.get(defaultMode, chalk, configFileOverride);
    await spectralValidator.setup(spectral, null, configObject);
  } catch (err) {
    return Promise.reject(err);
  }
 
  const swagger = await buildSwaggerObject(input);
 
  try {
    spectralResults = await spectral.run(input);
  } catch (err) {
    return Promise.reject(err);
  }
  const results = validator(swagger, configObject, spectralResults);
 
  // return a json object containing the errors/warnings
  return formatResultsAsObject(results);
};