- Custom Rulesets: Create custom rules to lint JSON or YAML objects
- Ready-to-use Rulesets: Validate and lint OpenAPI v2 & v3.x, AsyncAPI, and Arazzo v1 Documents
- API Style Guides: Automated API Style Guides using rulesets improve consistency across all your APIs
- Ready-to-use Functions: Built-in set of functions to help create custom rules. Functions include pattern checks, parameter checks, alphabetical ordering, a specified number of characters, provided keys are present in an object, etc.
- Custom Functions: Create custom functions for advanced use cases
- 🧰 Installation
- 💻 Usage
- 📖 Documentation
- ℹ️ Support
- 🌎 Real-World Rulesets
- ⚙️ Integrations
- 👏 Contributing
- 🌲 Sponsor Spectral by Planting a Tree
The easiest way to install spectral is to use either npm:
npm install -g @stoplight/spectral-cliOr yarn:
yarn global add @stoplight/spectral-cli
There are also additional installation options.
docker run --rm -it -v $(pwd):/tmp stoplight/spectral lint --ruleset "/tmp/.spectral.yaml" "/tmp/file.yaml"The Docker image sends a lightweight anonymous telemetry ping on startup (version + platform) to help us understand adoption. To opt out, set DO_NOT_TRACK=1 or SCARF_NO_ANALYTICS=true:
docker run --rm -it -e DO_NOT_TRACK=1 -v $(pwd):/tmp stoplight/spectral lint --ruleset "/tmp/.spectral.yaml" "/tmp/file.yaml"Spectral, being a generic YAML/JSON linter, needs a ruleset to lint files. A ruleset is a JSON, YAML, or JavaScript/TypeScript file (often the file is called .spectral.yaml for a YAML ruleset) that contains a collection of rules, which can be used to lint other JSON or YAML files such as an API description.
To get started, run this command in your terminal to create a .spectral.yaml file that uses the Spectral predefined rulesets based on OpenAPI, Arazzo or AsyncAPI:
echo 'extends: ["spectral:oas", "spectral:asyncapi", "spectral:arazzo"]' > .spectral.yamlIf you would like to create your own rules, check out the Custom Rulesets page.
Use this command if you have a ruleset file in the same directory as the documents you are linting:
spectral lint myapifile.yamlUse this command to lint with a custom ruleset, or one that's located in a different directory than the documents being linted:
spectral lint myapifile.yaml --ruleset myruleset.yaml- Documentation
- Getting Started - The basics of Spectral.
- Rulesets - Understand the structure of a ruleset so you can tweak and make your own rules.
Once you've had a look through the getting started material, some of these guides can help you become a power user.
- Different Workflows - When and where should you use Spectral? Editors, Git hooks, continuous integration, GitHub Actions, wherever you like!
- Using the command-line interface - Quickest way to get going with Spectral is in the CLI.
- Using the JavaScript API - Access the raw power of Spectral via the JS, or hey, TypeScript if you want.
- Custom Rulesets - Need something more than the core rulesets provide? Fancy building your own API Style Guide? Learn how to create a custom ruleset.
- Custom Functions - Handle more advanced rules, by writing a little JavaScript/TypeScript and calling it as a function.
If you need help using Spectral or have any questions, you can use GitHub Discussions, or visit the Stoplight Community Forum. These communities are a great place to share your rulesets, or show off tools that use Spectral.
If you have a bug or feature request, create an issue for it.
Stoplight has a set of Spectral rulesets that were created to help users get started with Stoplight's Style Guides. You can find them on API Stylebook, and you can download the source Spectral file by selecting a style guide on the project sidebar and selecting Export -> Spectral File(s) on the top-right. A few noteworthy style guides are:
- OWASP Top 10 - Set of rules to enforce OWASP security guidelines.
- URL Style Guidelines - Set of rules to help developers make better and consistent endpoints.
- Documentation - Scan an OpenAPI description to make sure you're leveraging enough of its features to help documentation tools like Stoplight Elements, ReDoc, and Swagger UI build the best quality API Reference Documentation possible.
Check out some additional style guides here:
- JetBrains Plugin - Automatic linting of your OpenAPI specifications and highlighting in your editor.
- Stoplight Studio - Uses Spectral to validate and lint OpenAPI documents.
- VS Code Spectral Extension - All the power of Spectral without leaving VS Code.
If you're using Spectral for an interesting use case, create an issue with details on how you're using it. We'll add it to a list here. Spread the goodness 🎉
If you are interested in contributing to Spectral, check out CONTRIBUTING.md.
- Mike Ralphson for kicking off the Spectral CLI and his work on Speccy