/spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v2 & v3.

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Demo of Spectral linting an OpenAPI document from the CLI CircleCI NPM Downloads Stoplight Forest

  • Custom Rulesets: Create custom rules to lint JSON or YAML objects
  • Ready-to-use Rulesets: Validate and lint OpenAPI v2 & v3 and AsyncAPI Documents
  • JSON Path Support: Use JSON path to apply rules to specific parts of your objects
  • 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
  • JSON Validation: Validate JSON with Ajv

Demo of Spectral linting an OpenAPI document from the CLI

Overview

🧰 Installation and Usage

Install

npm install -g @stoplight/spectral-cli

# OR

yarn global add @stoplight/spectral-cli

Find more installation methods in our documentation.

Create a Ruleset

Spectral, being a generic YAML/JSON linter, needs a ruleset to lint files. There are two ways to get a ruleset:

  1. Run this command to get our predefined rulesets based on OpenAPI or AsyncAPI:
printf '{\n  "extends": ["spectral:oas", "spectral:asyncapi"]\n}\n' > .spectral.json
  1. Create your own ruleset.

Lint

Use this command to lint with the predefined ruleset or a ruleset stored in the same directory as your API document:

spectral lint myapifile.yaml

Use this command to lint with a custom ruleset or one that is located in a different directory than your API document:

spectral lint myapifile.yaml --ruleset myruleset.json

📖 Documentation

ℹ️ Support

If you need help using Spectral or have any questions, please use GitHub Discussions, or visit the Stoplight Community Discord. These communities are a great place to share your rulesets, or show off tools that leverage Spectral.

If you have a bug or feature request, please create an issue.

❓ FAQs

How is this different to Ajv

Ajv is a JSON Schema validator, and Spectral is a JSON/YAML linter. Instead of just validating against JSON Schema, it can be used to write rules for any sort of JSON/YAML object, which could be JSON Schema, or OpenAPI, or anything similar. Spectral does expose a schema function that you can use in your rules to validate all or part of the target object with JSON Schema (we even use Ajv used under the hood for this), but that's just one of many functions.

I want to lint my OpenAPI documents but don't want to implement Spectral right now.

No problem! A hosted version of Spectral comes free with the Stoplight platform. Sign up for a free account here.

What is the difference between Spectral and Speccy

Speccy was a great inspiration for Spectral, but was designed to work only with OpenAPI v3. Spectral can apply rules to any JSON/YAML object (including OpenAPI v2/v3 and AsyncAPI). It's mostly been abandoned now, and is JavaScript not TypeScript.

⚙️ Integrations

🏁 Help Others Utilize Spectral

If you're using Spectral for an interesting use case, contact us for a case study. We'll add it to a list here. Spread the goodness 🎉

👏 Contributing

If you are interested in contributing to Spectral, check out CONTRIBUTING.md.

🎉 Thanks

📜 License

Spectral is 100% free and open-source, under Apache License 2.0.

🌲 Sponsor Spectral by Planting a Tree

If you would like to thank us for creating Spectral, we ask that you buy the world a tree.