π Why Lint and Validate Your API?
Validating your API is like spell-checking your OpenAPI file.
Linting is like code style enforcement for APIs.
β Catches missing fields, broken references, and syntax errors
β Ensures consistency across teams
β Prevents bad API design from making it to production
β Saves debugging time later
Now, letβs look at the tools that can help.
π Tools for Linting and Validating OpenAPI
Tool | Purpose | Pros |
---|---|---|
Spectral | Linting and style enforcement | Highly customizable, CI/CD friendly |
Swagger Editor | Validates OpenAPI specs | Quick and easy, web-based |
Redocly CLI | Validates OpenAPI for Redoc docs | Great for ReDoc users |
OpenAPI Generator Validator | Checks schema errors | CLI-based validation |
Stoplight Studio | GUI for API design and linting | No YAML needed, team collaboration |
Letβs set up and use these tools.
π 1. Validate OpenAPI with Swagger Editor (Quick & Easy)
If you just need a quick syntax check, Swagger Editor is the easiest way.
1οΈβ£ Go to editor.swagger.io
2οΈβ£ Paste your OpenAPI YAML
3οΈβ£ Errors will automatically appear in real-time
πΉ Pros: Free, web-based, instant feedback
πΉ Cons: No custom rules, not ideal for large projects
π₯ 2. Lint OpenAPI with Spectral (Best for CI/CD)
Spectral is the most powerful OpenAPI linting tool.
It enforces best practices and catches issues before deployment.
β Install Spectral:
|
|
β Run Spectral on Your OpenAPI File:
|
|
Example output:
|
|
π‘ Fix these issues before shipping your API!
π§ 3. Customize Spectral Rules (API Governance)
Spectral lets you define custom API linting rules in a .spectral.yaml
file.
Example .spectral.yaml
:
|
|
Now, running spectral lint openapi.yaml
will enforce your rules. π
πΉ Pros: Highly customizable, great for teams
πΉ Cons: Requires setup
π 4. Validate OpenAPI with Redocly CLI
If youβre using ReDoc for API docs, use Redocly CLI to ensure compatibility.
β Install Redocly:
|
|
β Validate OpenAPI:
|
|
This checks:
β Schema structure
β Documentation completeness
β Redoc-specific optimizations
πΉ Pros: Ideal for ReDoc users
πΉ Cons: Focused on API documentation, not API behavior
π 5. Automate Linting in CI/CD
Want every API change to be checked automatically?
Add Spectral to your CI/CD pipeline.
GitHub Actions Example:
Create .github/workflows/openapi-lint.yml
:
|
|
Now, every pull request will be automatically checked for OpenAPI errors. π
β Common OpenAPI Mistakes (and How to Fix Them)
Mistake | How to Fix |
---|---|
Missing descriptions | Add description to every endpoint & parameter |
No contact info | Ensure info.contact.email is present |
Duplicate operation IDs | Each endpoint should have a unique operationId |
Unused components | Remove unused schemas from components/schemas |
Invalid JSON references | Check $ref paths are correct |