How-to › Describe your API

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.

Audience
API producer
Level
beginner
Topic
Lint and govern API specs
Verified

You add a camel-case path to the OpenAPI file, the editor shows no squiggle, and the commit goes through. Twenty minutes later the pull request fails on a custom naming rule from the team ruleset. The editor extension never loaded that ruleset, so it checked the document against built-in rules that do not contain the one you broke.

What you get

You will end up with an editor, a commit hook, and a CI job sharing one ruleset and one command, plus a script that proves each layer reports a planted violation. This is for you if lint feedback reaches you only after you push.

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

ApproachWhen it fitsWhat it costs youWhen to pick something else
husky with lint-stagedA Node repository whose contributors all run npm installHooks install through a prepare script, and git commit --no-verify skips themContributors who never run npm install, or a repository with no package.json
pre-commitA repository with Python tooling or hooks in several languagesA Python install on every machine, plus a separate pre-commit install step per cloneThe team is Node only and would install Python for this hook alone
Redocly CLIRedocly already bundles or documents the APIIts own rule names, so every custom rule is written twice to match SpectralYour rules are Spectral rulesets with custom functions
Spectral VS Code extensionFeedback as you type, on the ruleset CI usesIts own bundled Spectral, and a silent fallback when the ruleset path is wrongAnyone 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:

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:

  "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 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:

{
  "spectral.rulesetFile": ".spectral.yaml",
  "spectral.run": "onType"
}

Set the path even though the file sits at the root. The extension’s README and its server code disagree about where it looks when the setting is empty.1 In 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:

npx lint-staged

lint-staged appends staged filenames to a command unless the configuration is a function, as its README explains. A function returning the CI command runs that command unchanged whenever a YAML file is staged:

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

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:

  /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:

node plant.mjs
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 rule, raised to error in repo/redocly.yaml. The script runs Redocly with REDOCLY_TELEMETRY=off, the switch its usage data page names.

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

node plant.mjs --show preCommit
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:

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')
})
node --test layers.test.mjs
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:

node consistency.mjs drifted
.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 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, 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.

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.

Footnotes

  1. The README of version 1.1.4 places the default ruleset “in the same folder as the document being validated” when the setting is empty. The function in 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. ↩︎ Back to text

  2. pre-commit renamed language: system to unsupported in version 4.4.0. Its documentation 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. ↩︎ Back to text

Read this page as markdown · All how-to guides

Generate the client instead of writing it#

Retries, timeouts, pagination and auth are the same problems in every client. Voxgig generates them from your OpenAPI description, in 23 languages, from one model.

Listen to Fireside, the Voxgig podcast

Since 2018, Richard Rodger has talked with developer advocates, event organizers, and the people who build developer-facing companies about the specifics of the work.