• Stars
    star
    2,503
  • Rank 18,340 (Top 0.4 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 21 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.

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.x and AsyncAPI 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

Overview

๐Ÿงฐ Installation

The easiest way to install spectral is to use either npm:

npm install -g @stoplight/spectral-cli

Or yarn:

yarn global add @stoplight/spectral-cli

There are also additional installation options.

๐Ÿ’ป Usage

1. Create a local ruleset

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 or AsyncAPI:

echo 'extends: ["spectral:oas", "spectral:asyncapi"]' > .spectral.yaml

If you would like to create your own rules, check out the Custom Rulesets page.

2. Lint

Use this command if you have a ruleset file in the same directory as the documents you are linting:

spectral lint myapifile.yaml

Use 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

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.

โ„น๏ธ Support

If you need help using Spectral or have any questions, you can use GitHub Discussions, or visit the Stoplight Community Discord. 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.

๐ŸŒŽ Real-World Rulesets

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.

There are also rulesets created by many companies to improve their APIs. You can use these as is to lint your OpenAPI descriptions, or use these as a reference to learn more about what rules you would want in your own ruleset:

  • Adidas - Adidas were one of the first companies to release their API Style Guide in a written guide and a Spectral ruleset. Lots of good rules to try in here.
  • APIs You Won't Hate - An opinionated collection of rules based on advice in the APIs You Won't Hate community.
  • Azure - Ruleset and complimentary style guide for creating OpenAPI 2 or 3 definitions of Azure services.
  • Box - Lots of Custom Functions being used to enforce good practices that the Box API governance folks are interested in.
  • DigitalOcean - Keeping their OpenAPI nice and tidy, enforcing use of $ref (probably to minimize conflicts), naming conventions for Operation IDs, and all sorts of other handy OpenAPI tips.
  • Tranascom - Don't even think about using anything other than application/json.
  • Zalando - Based on Zalando's RESTFUL API Guidelines, covers a wide-range of API topics such as versioning standards, property naming standards, the default format for request/response properties, and more.

Check out some additional style guides here:

โš™๏ธ Integrations

๐Ÿ Help Others Utilize Spectral

If you're using Spectral for an interesting use case, contact Stoplight for a case study. ๐ŸŽ‰

๐Ÿ‘ 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 Stoplight for creating Spectral, buy the world a tree.

More Repositories

1

prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
TypeScript
4,298
star
2

elements

Build beautiful, interactive API Docs with embeddable React or Web Components, powered by OpenAPI and Markdown.
TypeScript
1,789
star
3

studio

The modern editor for API Design and Technical Writing.
814
star
4

json-schema-viewer

A JSON Schema viewer React component
TypeScript
172
star
5

api-spec-converter

This package helps to convert between different API specifications (Postman, Swagger, RAML, StopLight).
JavaScript
137
star
6

spectral-action

GitHub Action wrapper for Spectral - a JSON/YAML/OpenAPI/AsyncAPI/etc linter with custom rule support.
JavaScript
89
star
7

desktop

The Stoplight desktop app + issues repository.
JavaScript
79
star
8

vscode-spectral

VS Code extension bringing the awesome Spectral JSON/YAML linter with OpenAPI/AsyncAPI support
TypeScript
70
star
9

spectral-owasp-ruleset

Improve the security of your API by detecting common vulnerabilities as defined by OWASP and enforced with Spectral.
TypeScript
68
star
10

spectral-rulesets

Keeping track of Spectral Rulesets in the wild, helping you build better, more consistent APIs, via OpenAPI, AsyncAPI, JSON Schema, etc.
53
star
11

json-ref-resolver

[Deprecated] Recursively resolve JSON pointers and remote authorities.
TypeScript
38
star
12

json

Useful functions when working with JSON.
TypeScript
21
star
13

http-spec

Utilities to normalize OpenAPI v2 and v3 objects for the Stoplight ecosystem.
TypeScript
19
star
14

ui-kit

Foundational React components for the Stoplight ecosystem.
CSS
13
star
15

formtron

JSON form builder for React.
TypeScript
13
star
16

jsonpath

JavaScript
12
star
17

yaml

Useful functions when working with YAML.
TypeScript
12
star
18

json-schema-tree

TypeScript
11
star
19

Public-APIs

A collection of public API descriptions.
10
star
20

scripts

Scripts to aid in testing, releasing, and linting.
TypeScript
8
star
21

json-schema-sampler

JavaScript
8
star
22

spectral-url-versioning

TypeScript
7
star
23

spectral-documentation

Teach Spectral how to help improve the quality of your OpenAPI-based documentation.
TypeScript
7
star
24

platform-docs

Documentation for the Stoplight Platform
7
star
25

studio-nightly

Nightly pre-releases of Studio Desktop https://github.com/stoplightio/studio
7
star
26

dev-portal

JavaScript
6
star
27

types

Common typings for the Stoplight ecosystem.
TypeScript
6
star
28

markdown

Useful functions when working with Markdown.
TypeScript
5
star
29

ExampleChooserPrismProxy

proxy server that adds example-selection logic to an upstream Stoplight Prism mock server
Python
5
star
30

elements-starter-angular

Angular starter for stoplight elements components
HTML
5
star
31

sample-specs

4
star
32

storybook-config

Shared storybook config for UI-Kit based project.
TypeScript
4
star
33

style-guides-rulebook-series

4
star
34

stoplight-app

4
star
35

studio-templates

Project templates for Stoplight Studio.
4
star
36

stoplight-cli-workflow

Stoplight CLI action
3
star
37

elements-starter-gatsby

Gatsby starter for Stoplight Elements components
JavaScript
3
star
38

js-coding-challenge

Stoplight Coding Challenge - HTTP request maker for OAS 2.0 specs
TypeScript
3
star
39

lifecycle

Event and disposable helpers.
TypeScript
3
star
40

elements-starter-react

[deprecated] Create React App example for older versions of Stoplight Elements. Use the new https://github.com/stoplightio/cra-template-elements
TypeScript
3
star
41

circleci-orbs

Stoplight's CircleCI Orbs
Shell
2
star
42

platform-v2-quickstart

2
star
43

gatsby-starter-elements

Gatsby Starter for Stoplight Elements
JavaScript
2
star
44

path

TypeScript
2
star
45

json-ref-readers

Paired with a resolver package, readers handle the loading of $ref content. This repo contains some common readers for files, URLs, etc.
TypeScript
2
star
46

deference-openapi

1
star
47

webpack

Webpack helpers, built on top of webpack-chain.
TypeScript
1
star
48

docs-gen3

1
star
49

code-of-conduct

Stoplight Community Code of Conduct
1
star
50

eslint-config

Shared Stoplight linting rules.
JavaScript
1
star
51

documentation

The markdown that powers the StopLight API Designer and Prism documentation.
1
star
52

cra-template-elements

Elements Create React App Template
HTML
1
star
53

react-error-boundary

TypeScript
1
star
54

docker-gitlab

From-source Docker image for Stoplight's fork of Gitlab on CentOS
Shell
1
star
55

gatsby-starter-elements-dev-portal

Gatsby Starter for Stoplight Elements Dev Portal
JavaScript
1
star
56

.github

Template files for all repos
1
star