# How to get lint feedback on an OpenAPI file while you edit it

> Point the Spectral VS Code extension and a commit hook at the ruleset CI uses, then plant a violation and confirm that every layer reports it.

Source: https://voxgig.com/howto/lint-openapi-while-you-edit

- Audience: api-producer
- Level: beginner
- Verified: 2026-11-29
- Published: 2026-11-29

## Short answer

Keep one Spectral ruleset file and one npm script that passes it with --ruleset. Set spectral.rulesetFile in .vscode/settings.json to that file, and have a husky and lint-staged hook, or a pre-commit framework hook, run the same script. Then plant a known violation: the CI command and the hook refuse it, and an editor that resolved no ruleset shows nothing, so a missing finding there means a wrong path.

---
## You will need

An OpenAPI 3 document, Node.js and npm. lint-staged 17.6.3 declares Node 22.22.1 or later in its
`engines` field. The pre-commit row needs Python 3. The sample keeps the reader's repository in
`repo/` and runs every layer in scratch git repositories, so nothing touches a real one.

## Approaches compared

| Approach | When it fits | What it costs you | When to pick something else |
| --- | --- | --- | --- |
| [husky with lint-staged](https://typicode.github.io/husky/get-started.html) | A Node repository whose contributors all run `npm install` | Hooks install through a `prepare` script, and `git commit --no-verify` skips them | Contributors who never run `npm install`, or a repository with no `package.json` |
| [pre-commit](https://pre-commit.com/#repository-local-hooks) | A repository with Python tooling or hooks in several languages | A Python install on every machine, plus a separate `pre-commit install` step per clone | The team is Node only and would install Python for this hook alone |
| [Redocly CLI](https://redocly.com/docs/cli/commands/lint) | Redocly already bundles or documents the API | Its own rule names, so every custom rule is written twice to match Spectral | Your rules are Spectral rulesets with custom functions |
| [Spectral VS Code extension](https://marketplace.visualstudio.com/items?itemName=stoplight.spectral) | Feedback as you type, on the ruleset CI uses | Its own bundled Spectral, and a silent fallback when the ruleset path is wrong | Anyone on another editor, or a rule that must hold before merge |

The extension answers as you type, but a mistyped path costs you the custom rules without any
error. husky and pre-commit run the exact CI command on every commit, and `--no-verify` skips
both. Redocly suits a team that has
standardized on it, at the price of a second rule file kept in step with the first.

## Name the ruleset in one script

The ruleset extends the built-in OpenAPI rules and adds one custom rule, the kind the editor
loses when it resolves the wrong file:

```yaml title="repo/.spectral.yaml"
extends: spectral:oas
rules:
  operation-description: error
  paths-kebab-case:
    description: Every literal path segment is lowercase kebab-case.
    message: "{{property}} has a segment that is not kebab-case"
    severity: error
    given: $.paths[*]~
    then:
      function: pattern
      functionOptions:
        match: "^(/([a-z0-9]+(-[a-z0-9]+)*|\\{[A-Za-z0-9]+\\}))+$"
```

One npm script carries the ruleset path, the failure level, and the document. CI, both hooks and
anyone at a terminal call it by name:

```json title="repo/package.json"
  "scripts": {
    "lint:openapi": "spectral lint --ruleset .spectral.yaml --fail-severity=error openapi.yaml",
    "prepare": "husky"
  },
  "scarfSettings": {
    "enabled": false
  },
```

`--ruleset` is explicit on purpose. Without it, the CLI looks for a `.spectral.*` file in its
working directory and stops with "No ruleset has been found" when there is none, as
[`getRuleset.ts`](https://github.com/stoplightio/spectral/blob/v6.17.1/packages/cli/src/services/linter/utils/getRuleset.ts)
in the Spectral source shows. Spectral depends on `@scarf/scarf`, which reports each install, and
the `scarfSettings` block turns that off for the whole project.

## Point the editor at the same file

The extension reads `spectral.rulesetFile` and resolves a relative path against the workspace
folder, which is the repository root when you open the repository:

```json title="repo/.vscode/settings.json"
{
  "spectral.rulesetFile": ".spectral.yaml",
  "spectral.run": "onType"
}
```

Set the path even though the file sits at the root. The extension's
[README](https://github.com/stoplightio/vscode-spectral/blob/v1.1.4/README.md#extension-settings) and its server code
disagree about where it looks when the setting is empty.[^1] In
[`server.ts`](https://github.com/stoplightio/vscode-spectral/blob/v1.1.4/server/src/server.ts),
a configured file that does not exist is not reported as an error. The server builds a ruleset
from the built-in `oas` and `asyncapi` sets instead. The sample keeps that fallback as
`editor-fallback.spectral.yaml`, because the extension cannot run here.

## Run the script from a commit hook

With husky, the `prepare` script sets `core.hooksPath`, and the hook file runs lint-staged:

```bash title="repo/.husky/pre-commit"
npx lint-staged
```

lint-staged appends staged filenames to a command unless the configuration is a function, as
its [README](https://github.com/lint-staged/lint-staged#using-js-configuration-files) explains.
A function returning the CI command runs that command unchanged whenever a YAML file is staged:

```js title="repo/lint-staged.config.mjs"
export default {
  '*.{yaml,yml}': () => 'npm run lint:openapi',
}
```

The pre-commit framework takes the same command as a repository-local hook.
`pass_filenames: false` stops it from adding filenames, and `language: unsupported` is the
name that version 4.4.0 gave to `system`:[^2]

```yaml title="repo/.pre-commit-config.yaml"
repos:
  - repo: local
    hooks:
      - id: openapi-lint
        name: OpenAPI lint, the CI command
        entry: npm run lint:openapi
        language: unsupported
        files: \.ya?ml$
        pass_filenames: false
```

The CI workflow, `repo/openapi-lint.yml`, ends in the step `run: npm run lint:openapi`.

## Plant a violation in every layer

The planted path breaks the custom rule only, since no rule in `spectral:oas` checks path casing:

```yaml title="violation.yaml"
  /invoices/{invoiceId}/lineItems:
```

`plant.mjs` copies `repo/` into scratch repositories with an empty git configuration and a fixed
identity. It runs each layer on a clean edit as the control, then on the violation:

```bash
node plant.mjs
```

```text output
layer                         clean edit                      planted violation
CI: npm run lint:openapi      exit 0, rule not reported       exit 1, paths-kebab-case
husky + lint-staged commit    committed, rule not reported    refused, paths-kebab-case
git commit --no-verify        not needed                      committed, rule not reported
pre-commit framework commit   committed, rule not reported    refused, paths-kebab-case
Redocly CLI lint              exit 0, rule not reported       exit 1, paths-kebab-case
editor fallback ruleset       exit 0, rule not reported       exit 0, rule not reported
```

Four layers refuse the violation. The fallback row shows what the extension lints when its path
is wrong: a document with a broken custom rule and a clean report. Redocly reports it through its
built-in [`paths-kebab-case`](https://redocly.com/docs/cli/rules/oas/paths-kebab-case) rule,
raised to `error` in `repo/redocly.yaml`. The script runs Redocly with `REDOCLY_TELEMETRY=off`,
the switch its [usage data page](https://redocly.com/docs/cli/usage-data) names.

The pre-commit hook prints the Spectral output of the CI script:

```bash
node plant.mjs --show preCommit
```

```text output
OpenAPI lint, the CI command.............................................Failed
- hook id: openapi-lint
- exit code: 1

> lint:openapi
> spectral lint --ruleset .spectral.yaml --fail-severity=error openapi.yaml


<scratch>/pre-commit/openapi.yaml
 57:35  error  paths-kebab-case  /invoices/{invoiceId}/lineItems has a segment that is not kebab-case  paths./invoices/{invoiceId}/lineItems

✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)
```

## Check it worked

The suite runs both edits through every layer and asserts each verdict, then runs the
consistency check described below on a good repository and on two broken ones:

```js title="layers.test.mjs"
test('every layer that runs the shared ruleset reports the planted violation', () => {
  const r = runLayers('planted')
  assert.equal(r.ci.status, 1)
  assert.ok(r.ci.reported, 'CI command')
  assert.equal(r.husky.committed, false)
  assert.ok(r.husky.reported, 'husky and lint-staged')
  assert.equal(r.preCommit.committed, false)
  assert.ok(r.preCommit.reported, 'pre-commit framework')
  assert.equal(r.redocly.status, 1)
  assert.ok(r.redocly.reported, 'Redocly CLI')
})
```

```bash
node --test layers.test.mjs
```

```text output
1..5
# tests 5
# suites 0
# pass 5
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 14466.644048
```

Lowering the custom rule to `warn` in `repo/.spectral.yaml` fails the first two tests, so the
suite depends on the rule and not on the layers happening to agree.

## When it goes wrong

The editor shows built-in warnings but no custom ones. The ruleset path resolved to a
file that does not exist, for example after one package of a monorepo was opened as the workspace. The
extension then lints with the built-in sets. Run a check that compares the setting with the
script, as `consistency.mjs` does:

```bash
node consistency.mjs drifted
```

```text output
.vscode/settings.json: spectral.rulesetFile is api/.spectral.yaml, the CI script uses .spectral.yaml
.vscode/settings.json: api/.spectral.yaml does not exist, and the extension falls back to its built-in rulesets without an error
package.json: @stoplight/spectral-cli is ^6.17.1, not one release
```

The file passes in the editor and fails on the branch with the same ruleset. The two runs used
different Spectral releases. The extension ships its own Spectral, declared in its server's
[`package.json`](https://github.com/stoplightio/vscode-spectral/blob/v1.1.4/server/package.json) as `@stoplight/spectral-rulesets` `^1.21.3`, while the CLI declares `>=1` for the
same package. Pin the CLI to one release and commit the lockfile, so the hook and CI resolve the
same rules. Treat the editor's report as a preview of theirs.

A commit with the violation reached the branch anyway. Someone ran
[`git commit --no-verify`](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---no-verify),
which bypasses the pre-commit hook, as the table's third row shows. Keep the CI job required.

## When not to do this

Do not make the hook the only gate. Any contributor can skip it, and a clone where nobody ran
`npm install` or `pre-commit install` has no hook at all. The required CI check is the layer that
holds.

Do not run the whole ruleset in a hook on a document that already fails it. Every commit is
refused until the backlog is fixed, which teaches the team `--no-verify`. Clean the base
branch first, or start the hook at `--fail-severity=error` with only the rules you enforce.

Skip the editor layer when the team uses several editors. A VS Code setting covers VS Code users
only, and its silent fallback teaches the rest of the team to trust a clean editor.

Avoid Redocly as the on-save layer when CI runs Spectral. Two rule vocabularies drift apart, so
the planted check has to cover both, rule by rule, on the same document.

## Related how-tos

- [Gate a pull request on OpenAPI lint results](/howto/gate-a-pull-request-on-openapi-lint)

- [Enforce a naming convention across 40 microservice specs](/howto/enforce-naming-conventions-across-many-specs)

- [Set up a Spectral lint loop for an agent editing OpenAPI](/howto/spectral-lint-loop-for-a-coding-agent)

- [Model an API in Smithy and emit OpenAPI with smithy-build](/howto/model-in-smithy-and-emit-openapi)

- [Test SDK generators on OpenAPI 3.1 before you upgrade](/howto/compare-generators-on-openapi-3-1-support)

## Last verified

Verified 2026-11-29 against Node 22.23.5, `@stoplight/spectral-cli` 6.17.1 with
`@stoplight/spectral-rulesets` 1.23.1, husky 9.1.7, lint-staged 17.6.3, `@redocly/cli` 2.84.0 and
pre-commit 4.7.0. Every output block is what the command preceding it printed. The Spectral VS
Code extension 1.1.4 was not run. Its settings and its fallback were read from its README and
its server source, and the fallback ruleset was run through the Spectral CLI instead.

[^1]: The [README of version 1.1.4](https://github.com/stoplightio/vscode-spectral/blob/v1.1.4/README.md#extension-settings) places the
default ruleset "in the same folder as the document being validated" when the setting is empty.
The function in
[`server.ts`](https://github.com/stoplightio/vscode-spectral/blob/v1.1.4/server/src/server.ts)
that does the looking carries a comment placing it "in the workspace folder" instead, and it
reads only that directory. The two descriptions agree only for a document at the root of the
workspace.

[^2]: pre-commit renamed `language: system` to `unsupported` in version 4.4.0. Its
[documentation](https://pre-commit.com/#unsupported) keeps `system` as an alias that "will be
removed in a future version" and says such hooks are for executables with no supported language.
Such a hook gets no virtual environment, and any dependency it needs is the user's to install by
hand.