@stoplight/spectral
A flexible object linter with out of the box support for OpenAPI v2 and v3.
Last updated 6 days ago by rossmcd .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @stoplight/spectral 
SYNC missed versions from official npm registry.

Spectral logo

Test Coverage Maintainability Build Status CircleCI

A flexible JSON linter with out of the box support for OpenAPI v2 and v3.

Demo of Spectral linting an OpenAPI document from the CLI

Features

  • Create custom rules to lint JSON or YAML objects
  • Ready to use rules to validate and lint OpenAPI v2 and v3 documents
  • Use JSON path to apply rules to specific parts of your objects
  • 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.
  • Create custom functions for advanced use cases
  • Validate JSON with Ajv

Installation

Spectral's CLI can be installed via npm or yarn.

npm install -g @stoplight/spectral

# OR

yarn global add @stoplight/spectral

Spectral is avaiable as a Docker image as well

docker run --rm -it stoplight/spectral lint "${url}"`

If the file you want to lint is on your computer, you'll need to mount the directory where the file resides as a volume

docker run --rm -it -v $(pwd):/tmp stoplight/spectral lint "/tmp/file.yaml"

Finally, executable binaries are also available.

Usage

CLI

After installing via one of the methods above, Spectral can be used via your command-line. Take a look at the CLI docs for information on the options available.

Examples

Concepts

There are three key concepts in Spectral: Rulesets, Rules, and Functions.

  • Rulesets act as a container for rules and functions.
  • Rules filter your object down to a set of target values, and specify the function that is used to evaluate those values.
  • Functions accept a value and return issue(s) if the value is incorrect.

Think of a Spectral ruleset as a flexible and customizable style guide for your JSON objects.

Programmatic usage

Spectral is written in TypeScript (then compiled to JavaScript) and can be used directly for when you need to use Spectral programmatically. Take a look at our "JavaScript API documentation".

FAQs

How is this different than Ajv?

Ajv is a JSON Schema validator, not a linter. 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 (Ajv is used under the hood). However, Spectral also provides a number of other functions and utilities that you can use to build up a linting ruleset to validates things that JSON Schema is not well suited for.

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?

With Spectral, lint rules can be applied to any JSON object. Speccy is designed to work with OpenAPI v3 only. The rule structure is different between the two. Spectral uses JSONPath path parameters instead of the object parameters (which are OpenAPI specific). Rules are also more clearly defined (thanks to TypeScript typings) and now require specifying a type parameter. Some rule types have been enhanced to be a little more flexible along with being able to create your own rules based on the built-in and custom functions.

Executable binaries

For users without Node and/or NPM/Yarn, we provide standalone packages for all major platforms. We also provide a shell script to auto download the executable based on your operating system:

curl -L https://raw.githack.com/stoplightio/spectral/master/install.sh | sh

Note, the binaries are not auto-updatable, therefore you will need to download a new version on your own.

Contributing

If you are interested in contributing to Spectral itself, check out our contributing docs to get started.

Also, most of the interesting projects are built with Spectral. Please consider using Spectral in a project or contribute to an existing one.

If you are using Spectral in your project and want to be listed in the examples section, we encourage you to open an issue.

Example Implementations

Helpful Links

  • JSONPath Online Evaluator, a helpful tool to determine what path you want
  • stoplightio/json, a library of useful functions for when working with JSON
  • stoplightio/yaml, a library of useful functions for when working with YAML, including parsing YAML into JSON, and a few helper functions such as getJsonPathForPosition or getLocationForJsonPath

Thanks :)

Support

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

If you need help using Spectral or have a support question, please use the Stoplight Community forum. We've created an open source category for these questions. It's also a great place to share your implementations.

If you want to discuss something in private, you can reach out to Stoplight support at support@stoplight.io.

Current Tags

  • 4.0.0-beta.10                                ...           beta (6 days ago)
  • 4.0.0                                ...           latest (6 days ago)

52 Versions

  • 4.0.0                                ...           6 days ago
  • 4.0.0-beta.10                                ...           6 days ago
  • 4.0.0-beta.9                                ...           6 days ago
  • 4.0.0-beta.8                                ...           7 days ago
  • 4.0.0-beta.7                                ...           8 days ago
  • 4.0.0-beta.6                                ...           10 days ago
  • 4.0.0-beta.4                                ...           10 days ago
  • 0.0.0                                ...           11 days ago
  • 4.0.0-beta.3                                ...           11 days ago
  • 4.0.0-beta1                                ...           18 days ago
  • 3.1.0                                ...           a month ago
  • 3.0.5                                ...           a month ago
  • 3.0.4                                ...           a month ago
  • 3.0.3                                ...           2 months ago
  • 3.0.2                                ...           2 months ago
  • 3.0.1                                ...           2 months ago
  • 3.0.0                                ...           2 months ago
  • 2.4.0                                ...           2 months ago
  • 2.3.2                                ...           2 months ago
  • 2.3.1                                ...           2 months ago
  • 2.3.0                                ...           2 months ago
  • 2.2.0                                ...           2 months ago
  • 2.1.1                                ...           2 months ago
  • 2.1.0                                ...           3 months ago
  • 2.0.6                                ...           4 months ago
  • 2.0.5                                ...           4 months ago
  • 2.0.4                                ...           4 months ago
  • 2.0.3                                ...           4 months ago
  • 2.0.2                                ...           4 months ago
  • 2.0.1                                ...           4 months ago
  • 2.0.0                                ...           4 months ago
  • 1.2.0                                ...           4 months ago
  • 1.1.2                                ...           4 months ago
  • 1.1.1                                ...           4 months ago
  • 1.1.0                                ...           5 months ago
  • 1.0.2                                ...           5 months ago
  • 1.0.1                                ...           7 months ago
  • 1.0.0                                ...           7 months ago
  • 0.1.2-beta.6                                ...           7 months ago
  • 0.1.2-beta.5                                ...           7 months ago
  • 0.1.2-beta.4                                ...           7 months ago
  • 0.1.2-beta.3                                ...           7 months ago
  • 0.1.2-beta.2                                ...           7 months ago
  • 0.1.2-beta.1                                ...           9 months ago
  • 0.1.1                                ...           9 months ago
  • 0.1.0                                ...           9 months ago
  • 0.1.0-beta.5                                ...           9 months ago
  • 0.1.0-beta.4                                ...           9 months ago
  • 0.1.0-beta.3                                ...           9 months ago
  • 0.1.0-beta.2                                ...           9 months ago
  • 0.1.0-beta.1                                ...           9 months ago
  • 0.1.1-beta.1                                ...           9 months ago
Downloads
Today 0
This Week 52
This Month 775
Last Day 52
Last Week 364
Last Month 254
Dependencies (18)
Dev Dependencies (32)

Copyright 2014 - 2016 © taobao.org |