stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.5k stars 238 forks source link

cannot override based on formats #2446

Open rittneje opened 1 year ago

rittneje commented 1 year ago

For support questions, please use the Stoplight Discord Community. This repository's issues are reserved for feature requests and bug reports. If you are unsure if you are experiencing a bug, our Discord is a great place to start.

Please delete this section, any any sections below that you don't use, before creating the issue.

Describe the bug

The docs say you can apply overrides to particular formats. https://docs.stoplight.io/docs/spectral/293426e270fac-overrides

However, there does not seem to be a way to do this properly.

If I create a config like so:

extends:
- "spectral:oas"
- "spectral:asyncapi"
overrides:
- formats: ["oas3_0"]
  rules:
    operation-description: "info"

I get an error

Error running Spectral!
Error #1: must have required property 'files'
          at                     …./../../snapshot/project/packages/core/src/ruleset/validation/errors.ts:85      return new RulesetValidationError(inferErrorCode(path, error.keyword), error.message ?? 'unknown error', path)…
          at flatMa…                                                                                                                                                                                                             
          at convertAjvError…    …./../../snapshot/project/packages/core/src/ruleset/validation/errors.ts:79      return filteredErrors.flatMap(error => …                                                                       
          at assertValidRulese…  …./../../snapshot/project/packages/core/src/ruleset/validation/assertions.ts:26  throw new AggregateError(convertAjvErrors(validate.errors ?? []))…                                             
          at new Rulese…         …./../../snapshot/project/packages/core/src/ruleset/ruleset.ts:64                assertValidRuleset({ extends: [], ...def })…                                                                   

As per the error, files is required, even though I don't intend to filter by file name.

If I try to add something for files, it seems to do a union rather than an intersection.

extends:
- "spectral:oas"
- "spectral:asyncapi"
overrides:
- files: ["*"]
  formats: ["oas3_0"]
  rules:
    operation-description: "info"

That is, it applies the override to every file, regardless of format.

To Reproduce

See above.

Expected behavior

There should be a way to override rules for particular formats. Specifying both files and formats should use intersection logic not union logic. (If union logic is desired you should just make two entries.)

Environment (remove any that are not applicable):

P0lip commented 1 year ago

We currently don't allow overrides based on formats alone. The documentation is possibly a tad misleading here. As things stand, you always need to specify files.