spectral icon indicating copy to clipboard operation
spectral copied to clipboard

fails to run base on js format ruleset

Open jianyexi opened this issue 3 years ago • 2 comments

Describe the bug A clear and concise description of what the bug is.

To Reproduce

  1. Given this ruleset file
> cat myruleset.js 
import { oas2, oas3 } from "https://cdn.jsdelivr.net/npm/@stoplight/spectral-formats/+esm";
import { falsy } from "https://cdn.jsdelivr.net/npm/@stoplight/[email protected]/+esm";
import { oas } from "https://cdn.jsdelivr.net/npm/@stoplight/spectral-rulesets/+esm";
const ruleset = {
    extends: [
        oas
    ],
    rules: {
        "info-contact": "off",
        "no-$ref-siblings": "off",
        "additional-properties-and-properties": {
            "description": "Don't specify additionalProperties as a sibling of properties.",
            "severity": "warn",
            "formats": [oas2, oas3],
            "given": "$..[?(@object() && @.type === 'object' && @.properties)]",
            "then": {
                "field": "additionalProperties",
                "function": falsy
            }
        },
    }
};
export default ruleset;
  1. run
> spectral lint -r myruleset.js  swagger.json
  1. See error
Error running Spectral!
Error #1: 'isObject' is not exported by https://cdn.jsdelivr.net/npm/[email protected]/+esm, imported by https://cdn.jsdelivr.net/npm/@stoplight/[email protected]/+esm
          at net/npm/@stoplight/…
          at error                 …hared/rollup.js:160  base = Object.assig…
          at error                 …red/rollup.js:12436  return error(props);
          at traceVariable         …red/rollup.js:12797  return this.error(e…
          at findVariable          …red/rollup.js:11419  const variable = th…

Expected behavior return the linting result

jianyexi avatar Mar 24 '22 07:03 jianyexi

add another case:

  1. Given this remote ruleset file
> cat    https://foo/myruleset.js 
var spectralFormats = require('@stoplight/spectral-formats');
var spectralFunctions = require('@stoplight/spectral-functions');
const ruleset = {
    extends: [
    ],
    rules: {
        "info-contact": "off",
        "no-$ref-siblings": "off",
        "additional-properties-and-properties": {
            "description": "Don't specify additionalProperties as a sibling of properties.",
            "severity": "warn",
            "formats": [spectralFormats .oas2, spectralFormats .oas3],
            "given": "$..[?(@object() && @.type === 'object' && @.properties)]",
            "then": {
                "field": "additionalProperties",
                "function": spectralFunctions .falsy
            }
        },
    }
};
module.exports = ruleset
  1. run
> spectral lint -r https://foo/myruleset.js  swagger.json
  1. See error
Error running Spectral!
Error #1: '@stoplight/spectral-formats' is imported as an external by @stoplight/spectral-formats?commonjs-proxy, but is already an existing non-external module id.
          at Error: '@stoplight/…
          at error                 …hared/rollup.js:160  base = Object.assig…
          at fetchResolvedDepend…  …red/rollup.js:22281  return error(errInt…
          at                       …red/rollup.js:22290  for (const dependen…
          at processTicksAndReje…  …s/task_queues.js:93

jianyexi avatar Mar 25 '22 02:03 jianyexi

I had all sorts of trouble with the JS format as documented, so I wrote this article to show how it can work, and have a public NPM package you can copy.

https://apisyouwonthate.com/blog/distribute-spectral-style-guides-with-npm#publish-your-spectral-style-guide-to-npm

@heitortsergent can we update docs to show a working example?

@mnaumanali94 it's worth noting that the examples for JS format we have don't work.

philsturgeon avatar Aug 06 '22 08:08 philsturgeon