Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stoplight/spectral

Package Overview
Dependencies
Maintainers
10
Versions
107
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/spectral - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

31

package.json
{
"name": "@stoplight/spectral",
"version": "1.2.0",
"version": "2.0.0",
"description": "A flexible object linter with out of the box support for OpenAPI v2 and v3.",

@@ -18,3 +18,4 @@ "keywords": [

"files": [
"**/*"
"./*",
"src/**/*"
],

@@ -31,9 +32,27 @@ "author": "Stoplight <support@stoplight.io>",

"dependencies": {
"@stoplight/json": "1.x.x",
"@stoplight/types": "3.x.x",
"@oclif/command": "^1.0",
"@oclif/plugin-help": "^2.0",
"@stoplight/json": "^1.8.0",
"@stoplight/json-ref-resolver": "^1.2.2",
"@stoplight/types": "^4.0.0",
"@stoplight/yaml": "^2.1.1",
"ajv": "6.x.x",
"chalk": "^2.4.2",
"jsonpath": "https://github.com/stoplightio/jsonpath#78140eb1980d4ff385780e2fa12177735685ec3e",
"lodash": ">=4.17.5"
"lodash": ">=4.17.5",
"node-fetch": "2.x.x",
"strip-ansi": "^5.2.0",
"text-table": "^0.2.0"
},
"typings": "index.d.ts"
"typings": "index.d.ts",
"bin": {
"spectral": "./bin/run"
},
"oclif": {
"commands": "./cli/commands",
"bin": "spectral",
"plugins": [
"@oclif/plugin-help"
]
}
}

210

README.md

@@ -6,12 +6,12 @@ ![Spectral logo](img/spectral-banner.png)

A flexible JSON object linter with out of the box support for OpenAPI Specification 2 and 3
A flexible JSON object linter with out of the box support for OpenAPI v2 and v3
## Features
- Create custom rules to lint _any JSON object_.
- Use JSON paths to apply rules / functions to specific parts of your JSON objects.
- Built-in set of functions to help build 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.
- Optional ready to use rules and functions to validate and lint OpenAPI Specification (OAS) 2 _and_ 3 documents.
- Validate JSON with [Ajv](https://github.com/epoberezkin/ajv).
- Create custom rules to lint _any JSON object_
- Use JSON paths to apply rules / functions to specific parts of your JSON objects
- Built-in set of functions to help [build custom rules](#creating-a-custom-rule). Functions include pattern checks, parameter checks, alphabetical ordering, a specified number of characters, provided keys are present in an object, etc
- [Create custom functions](#creating-a-custom-function) for advanced use cases
- Optional ready to use rules and functions to validate and lint [OpenAPI v2 _and_ v3 documents](#example-linting-an-openapi-document)
- Validate JSON with [Ajv](https://github.com/epoberezkin/ajv)

@@ -24,6 +24,77 @@ ## Installation

Supports Node v8.3+ and modern browsers.
Supports Node v8.3+.
## Usage
### CLI
Spectral can be run via the command-line:
```bash
spectral lint petstore.yaml
```
Other options include:
``` text
-e, --encoding=encoding [default: utf8] text encoding to use
-f, --format=json|stylish [default: stylish] formatter to use for outputting results
-h, --help show CLI help
-m, --maxResults=maxResults [default: all] maximum results to show
-o, --output=output output to a file instead of stdout
-v, --verbose increase verbosity
```
> Note: The Spectral CLI supports both YAML and JSON.
Currently, the CLI supports validation of OpenAPI documents and lints them based on our default ruleset. It does not support custom rulesets at this time. Although if you want to build and run custom rulesets outside of the CLI, see [Customization](#Customization).
### Example: Linting an OpenAPI document
Spectral includes a number of ready made rules and functions for OpenAPI v2 and v3 documents.
This example uses the OpenAPI v3 rules to lint a document.
```javascript
const { Spectral } = require('@stoplight/spectral');
const { oas3Functions, oas3Rules } = require('@stoplight/spectral/rulesets/oas3');
// an OASv3 document
const myOAS = {
// ... properties in your document
responses: {
'200': {
description: '',
schema: {
$ref: '#/definitions/error-response',
},
},
},
// ... properties in your document
};
// create a new instance of spectral with all of the baked in rulesets
const spectral = new Spectral();
spectral.addFunctions(oas3Functions());
spectral.addRules(oas3Rules());
spectral.addRules({
// .. extend with your own custom rules
});
// run!
spectral.run(myOAS).then(results => {
console.log(JSON.stringify(results, null, 4));
});
```
You can also [add to these rules](#Creating-a-custom-rule) to create a customized linting style guide for your OpenAPI documents.
The existing OAS rules are opinionated. There might be some rules that you prefer to change. We encourage you to create your rules to fit your use case. We welcome additions to the existing rulesets as well!
## Advanced
### Customization
There are two key concepts in Spectral: **Rules** and **Functions**.

@@ -36,3 +107,3 @@

### Creating a custom rule
#### Creating a custom rule

@@ -62,25 +133,21 @@ Spectral has a built-in set of functions which you can reference in your rules. This example uses the `RuleFunction.PATTERN` to create a rule that checks that all property values are in snake case.

const results = spectral.run({
name: 'helloWorld',
// run!
spectral.run({name: 'helloWorld',}).then(results => {
console.log(JSON.stringify(results, null, 4));
});
console.log(JSON.stringify(results, null, 4));
// => outputs a single result since `helloWorld` is not snake_case
// {
// "results": [
// {
// "name": "snake_case",
// "message": "must match the pattern '^[a-z]+[a-z0-9_]*[a-z0-9]+$'",
// "severity": 40,
// "severityLabel": "warn",
// "path": [
// "name"
// ]
// }
// ]
// }
// [
// {
// "code": "snake_case",
// "message": "must match the pattern '^[a-z]+[a-z0-9_]*[a-z0-9]+$'",
// "severity": 1,
// "path": [
// "name"
// ]
// }
// ]
```
### Creating a custom function:
#### Creating a custom function

@@ -132,70 +199,22 @@ Sometimes the built-in functions don't cover your use case. This example creates a custom function, `customNotThatFunction`, and then uses it within a rule, `openapi_not_swagger`. The custom function checks that you are not using a specific string (e.g., "Swagger") and suggests what to use instead (e.g., "OpenAPI").

const results = spectral.run({
description: 'Swagger is pretty cool!',
// run!
spectral.run({description: 'Swagger is pretty cool!',}).then(results => {
console.log(JSON.stringify(results, null, 4));
});
console.log(JSON.stringify(results, null, 4));
// => outputs a single result since we are using the term `Swagger` in our object
// {
// "results": [
// {
// "name": "openapi_not_swagger",
// "message": "Use OpenAPI instead of Swagger!",
// "severity": 40,
// "severityLabel": "warn",
// "path": [
// "description"
// ]
// }
// ]
// }
// [
// {
// "code": "openapi_not_swagger",
// "message": "Use OpenAPI instead of Swagger!",
// "severity": 1,
// "path": [
// "description"
// ]
// }
// ]
```
### Linting an OAS 2 document
Spectral also includes a number of ready made rules and functions for OpenAPI Specification (OAS) 2 and 3 documents. This example uses the OAS 2 rules to lint a document.
You can also add to these rules to create a customized linting style guide for your OAS documents.
```javascript
const { Spectral } = require('@stoplight/spectral');
const { oas2Functions, oas2Rules } = require('@stoplight/spectral/rulesets/oas2');
// an OASv2 document
var myOAS = {
// ... properties in your document
responses: {
'200': {
description: '',
schema: {
$ref: '#/definitions/error-response',
},
},
},
// ... properties in your document
};
// create a new instance of spectral with all of the baked in rulesets
const spectral = new Spectral();
spectral.addFunctions(oas2Functions());
spectral.addRules(oas2Rules());
spectral.addRules({
// .. extend with your own custom rules
});
// run!
const results = spectral.run(myOAS);
console.log(JSON.stringify(results, null, 4));
```
Note: The existing OAS rules are opinionated. There might be some rules that you prefer to change. We encourage you to create your rules to fit your use case. We welcome additions to the existing rulesets as well!
### Example Implementations
- [Spectral Bot](https://github.com/tbarn/spectral-bot), a GitHub pull request bot that lints your repo's OAS document that uses the [Probot](https://probot.github.io) framework, built by [Taylor Barnett](https://github.com/tbarn)
## FAQs

@@ -207,3 +226,3 @@

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

@@ -214,7 +233,7 @@ No problem! A hosted version of Spectral comes **free** with the Stoplight platform. Sign up for a free account [here](https://stoplight.io/?utm_source=github&utm_campaign=spectral).

With Spectral, lint rules can be applied to _any_ JSON object, not just OAS 3 documents. The rule structure is different between the two. Spectral uses [JSONPath](http://goessner.net/articles/JsonPath/) `path` parameters instead of the `object` parameters (which are OAS-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.
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](http://goessner.net/articles/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.
## Contributing
If you are interested in contributing to Spectral itself, check out our [contributing docs](CONTRIBUTING.md) [Coming soon!] to get started.
If you are interested in contributing to Spectral itself, check out our [contributing docs](CONTRIBUTING.md) to get started.

@@ -225,2 +244,8 @@ Also, most of the interesting projects are built _with_ Spectral. Please consider using Spectral in a project or contribute to an [existing one](#example-implementations).

### Example Implementations
- [Stoplight's Custom Style and Validation Rules](https://docs.stoplight.io/modeling/modeling-with-openapi/style-validation-rules) uses Spectral to validate and lint OpenAPI documents on the Stoplight platform
- [Spectral GitHub Bot](https://github.com/tbarn/spectral-bot), a GitHub pull request bot that lints your repo's OpenAPI document that uses the [Probot](https://probot.github.io) framework, built by [Taylor Barnett](https://github.com/tbarn)
- [Spectral GitHub Action](https://github.com/XVincentX/spectral-action), a GitHub Action that lints your repo's OpenAPI document, built by [Vincenzo Chianese](https://github.com/XVincentX/)
## Helpful Links

@@ -230,3 +255,3 @@

- [stoplightio/json](https://github.com/stoplightio/json), a library of useful functions for when working with JSON
- [stoplightio/yaml](https://github.com/stoplightio/yaml), a library of useful functions for when working with YAML, including parsing YAML into JSON with a source map that includes JSONPath pointers for every property in the result
- [stoplightio/yaml](https://github.com/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`

@@ -236,2 +261,3 @@ ## Thanks :)

- [Phil Sturgeon](https://github.com/philsturgeon) for collaboration and creating Speccy
- [Mike Ralphson](https://github.com/MikeRalphson) for kicking off the Spectral CLI

@@ -238,0 +264,0 @@ ## Support

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc