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

Ability to suppress false positives #2457

Closed tillig closed 1 year ago

tillig commented 1 year ago

User story.

As a developer, I can suppress a specific rule violation, so that I can ignore false positives or exceptions to the rules.

Is your feature request related to a problem?

There are times when you have a linting rule that you definitely want applied everywhere, except there's a schema with one operation that just can't conform. Maybe it can't be changed, maybe you can't quite get the regex right, but whatever the case is, that one violation really isn't a violation. It needs to be suppressed or otherwise ignored.

However, you don't want to turn the whole rule off, because you want to keep linting everything else with that rule. You just need the line-item suppression.

Describe the solution you'd like

A way to configure a set of suppressions for a given schema. This may take the form of...

...or some other mechanism to indicate "don't warn/err on this specific violation."

Additional context

IRuleResult has a path (JsonPath) property, so the source of the error is known. Perhaps that's the way to specify the exclusion - a JsonPath value along with a list of rules to suppress at that site.

Alternatively, I've seen some systems generate a "fingerprint" based on the file, source location, etc., some sort of hash - that might be a second step, though I think it'd be a more far-reaching change than just using the JsonPath value.

I did see that #1145 was opened a couple years ago and then closed, but it doesn't look like anything was addressed. This is sort of a duplicate of that one.

anikitin commented 1 year ago

Doesn't https://docs.stoplight.io/docs/spectral/293426e270fac-overrides help to achieve what you are looking for? We use overrides to switch some rule severity to "off" for particular spec files/paths.

tillig commented 1 year ago

This is an interesting idea. Let me try it tomorrow and I'll post back here with an update. Thanks!

tillig commented 1 year ago

This works perfectly. In particular, the example that shows how to override a particular path and turn the rule off is what I needed.

overrides:
  - files:
      - "**/*.json#/paths/~1path~1to~1operation"
    rules:
      my-rule-that-does-not-apply: "off"
anikitin commented 1 year ago

@tillig , just keep in mind that JSON pointer reference syntax is changed in latest version of spectral. You will need to escape some characters (for example curly brackets) in paths when defining your overrides. It was not required before. See #2515 for details.