What is Spectral (Stoplight)?
Spectral is an open-source (Apache 2.0) linter for JSON and YAML, built by Stoplight, now part of SmartBear. On its own it knows nothing; it applies a ruleset, usually a .spectral.yaml file, in which each rule selects parts of a document with JSONPath and checks them with a function such as truthy, pattern, casing or schema. It ships ready-made rulesets for OpenAPI 2.0, 3.0, 3.1 and 3.2 (spectral:oas), AsyncAPI (spectral:asyncapi) and Arazzo 1.x (spectral:arazzo), and runs as a CLI, a Docker image or a JavaScript library. It also powers linting in the Stoplight platform’s editor.
Why use it in the API Lifecycle?
- Style guides as code: Encode naming, casing, pagination, error-shape and versioning conventions as rules, so reviews stop relitigating them.
- Ready-made OpenAPI checks:
spectral:oascovers rules such asoperation-operationId-unique,path-params,oas3-valid-media-example(examples must match their schemas) andoas3-unused-component. - Extensible: Rulesets can
extendsother rulesets, including ones loaded by URL, and custom functions are written in JavaScript. - CI-friendly output: Formatters include
github-actions,sarif,junit,gitlabandjson, and--fail-severitydecides which level breaks the build. - Beyond OpenAPI: Because it is a generic JSON/YAML linter, the same engine can check AsyncAPI, Arazzo, or any other structured file a team maintains.
Best Practices
- Commit a
.spectral.yamlthat extendsspectral:oasto each API repository (or extends one shared ruleset by URL), and runspectral lint openapi.yaml --fail-severity=warnon every pull request. Linting in an editor only helps the person who has the extension installed. - Write rules for the conventions you actually enforce in review, with a
messageand adescriptionthat explain why. Good candidates: every operation documents a 4xx response, error responses useapplication/problem+jsonper Problem Details, collection endpoints declare the pagination parameters from Pagination, and paths follow your resource naming casing. - Start a new ruleset at
warn, fix the backlog, then promote rules toerror. Turning on a strict ruleset aterroron day one gets the linter disabled, not the API fixed. - Keep
oas3-valid-media-exampleandoas3-valid-schema-exampleon. Examples that do not match their schemas mislead every reader of the docs and break any mock server that serves them, such as Prism. - Do not use a linter as a substitute for detecting breaking changes. Spectral checks one version of a document in isolation; comparing two versions is a different job (see What Counts as a Breaking Change).
- Keep the ruleset in YAML rather than a JavaScript module unless you need code: YAML rulesets also run in vacuum, a Go reimplementation, so you are not locked to one engine. If the API will be called by agents, add legible, which checks different things. Rule design and rollout are covered in Design-First Development and API Governance.