This guide explains how to lint the OpenAPI specifications locally using Spectral.
You need to have Spectral CLI installed. If you don't have it:
# Using npm (recommended)
npm install -g @stoplight/spectral-cli
# Using yarn
yarn global add @stoplight/spectral-cliVerify installation:
spectral --versionRun the provided shell script to lint all API specifications:
./lint-api.shThis will automatically:
- Check if Spectral is installed
- Lint both
records-editor.yamlandmarc-specifications.yaml - Show a summary of results
Lint individual files:
# Lint records-editor API
spectral lint src/main/resources/swagger.api/records-editor.yaml
# Lint marc-specifications API
spectral lint src/main/resources/swagger.api/marc-specifications.yaml
# Lint both files at once
spectral lint src/main/resources/swagger.api/*.yaml# Quiet mode (only show errors)
spectral lint --quiet src/main/resources/swagger.api/records-editor.yaml
# JSON output
spectral lint --format json src/main/resources/swagger.api/records-editor.yaml
# Show full error details
spectral lint --verbose src/main/resources/swagger.api/records-editor.yaml
# Lint and ignore specific rules
spectral lint --ignore-unknown-format src/main/resources/swagger.api/records-editor.yamlThe project uses .spectral.yaml configuration file which:
- Extends the standard OpenAPI ruleset:
spectral:oas - Custom rules:
docs-descriptions: Ensures descriptions are provided and properly formatteddocs-info-contact: Requires contact information in the API specdocs-summary: Requires summaries for all operationsdocs-parameters-examples-or-schema: Parameters must have examples or schemasdocs-media-types-examples-or-schema: Media types must have examples or schemasdocs-tags-alphabetical: Tags should be in alphabetical orderdocs-operation-tags: Operations must have at least one tag
- error: Must be fixed (breaks build in CI)
- warn: Should be fixed (reported but doesn't break build)
- info: Nice to have
- hint: Suggestions
# ❌ Wrong
servers:
- url: /records-editor/
# ✅ Correct
servers:
- url: /records-editor# Add global tags section
tags:
- name: records-editor
description: Operations for managing MARC records
paths:
/records:
get:
tags:
- records-editor # Now this is defined# ❌ Wrong
info:
version: 5.5
# ✅ Correct
info:
version: "5.5"# ❌ Wrong
properties:
tag:
type: string
example: 100 # Number
# ✅ Correct
properties:
tag:
type: string
example: "100" # StringThe GitHub Actions workflow .github/workflows/api-lint.yml automatically runs linting on:
- Every push to branches with API changes
- Every pull request with API changes
You can also trigger it manually via GitHub Actions UI.
For real-time linting in VS Code:
-
Install the Spectral extension:
- Extension ID:
stoplight.spectral
- Extension ID:
-
The extension will automatically use
.spectral.yamlconfiguration -
You'll see linting errors/warnings inline as you edit OpenAPI files
For IntelliJ IDEA / WebStorm:
- Install the OpenAPI plugin (usually pre-installed)
- Configure Spectral as an external tool:
- Go to Settings → Tools → External Tools
- Add a new tool:
- Name:
Spectral Lint - Program:
spectral - Arguments:
lint $FilePath$ - Working directory:
$ProjectFileDir$
- Name:
To automatically lint before commits, add to .git/hooks/pre-commit:
#!/bin/sh
echo "Running API lint..."
./lint-api.shMake it executable:
chmod +x .git/hooks/pre-commit# Check Spectral version
spectral --version
# Get help
spectral lint --help
# Validate the .spectral.yaml ruleset itself
spectral lint --ruleset .spectral.yaml --test
# Generate HTML report
spectral lint --format html src/main/resources/swagger.api/records-editor.yaml > api-lint-report.html