1

I try to lint my open api spec with spectral using its javascript api but it always returns an empty array, even when my spec is definitely invalid.

This is the Jest test I am using to test my rules:

const myLinter = require('#root/src/main/linter.js');
test('will it work?', async () => {
   await myLinter.lint("open-api.yaml", "rules.yaml");
});

The linter.js looks like this:

const { fetch } = require('@stoplight/spectral-runtime')
const { Spectral, Document } = require('@stoplight/spectral-core')
const Parsers = require('@stoplight/spectral-parsers')
const fs = require('fs')
const path = require('path')
const { bundleAndLoadRuleset } = require('@stoplight/spectral-ruleset-bundler/with-loader')
const { DiagnosticSeverity } = require('@stoplight/types')

const lint = async (openApiFileName, rulesetFileName) => {
    const ruleset = await bundleAndLoadRuleset(path.resolve(path.join('src/test', rulesetFileName)), { fs, fetch });
    const spectral = new Spectral();
    spectral.setRuleset(ruleset);

    const resolved = path.resolve(path.join('src/test', openApiFileName));
    const body = fs.readFileSync(resolved, 'utf8');
    let document = new Document(body, Parsers.Yaml, resolved);

    let results = await spectral.run(document);
    console.log(results);
}

module.exports = {lint}

The ruleset is loaded successfully. I can see this in the debugger, so I know that all the paths are correct. The only rule I have for testing is:

rules:
  resource-names-plural:
    severity: warn
    message: "Resource names should generally be plural"
    given: "$.paths"
    then:
      function: pattern
      functionOptions:
        match: "^hello$"

And my open api spec does contain only paths different from "hello". Nonetheless, it runs without returning any results. The returning array is empty.

What am I doing wrong?

Tobias Neubert
  • 141
  • 1
  • 10

0 Answers0