Skip to content

Commit 7c66ca4

Browse files
author
Mike Kistler
committed
docs(spectral): add doc on Spectral configuration and custom rules
1 parent db60c11 commit 7c66ca4

File tree

1 file changed

+115
-23
lines changed

1 file changed

+115
-23
lines changed

README.md

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,49 @@ Support for Node v8 is deprecated. Support will be officially dropped when it re
1919
- NPM 5.x
2020

2121
## Table of contents
22-
- [Prerequisites](#prerequisites)
22+
23+
<!--
24+
The TOC below is generated using the `markdown-toc` node package.
25+
26+
https://github.com/jonschlinkert/markdown-toc
27+
28+
You should regenerate the TOC after making changes to this file.
29+
30+
markdown-toc --maxdepth 4 -i README.md
31+
-->
32+
33+
<!-- toc -->
34+
2335
- [Installation](#installation)
24-
- [NPM](#install-with-npm-recommended)
25-
- [Source](#build-from-source)
36+
* [Install with NPM (recommended)](#install-with-npm-recommended)
37+
* [Build from source](#build-from-source)
38+
* [Platform specific binaries](#platform-specific-binaries)
39+
* [Docker container](#docker-container)
2640
- [Usage](#usage)
27-
- [Command line](#command-line)
28-
- [Node module](#node-module)
29-
- [API](#api)
30-
- [Structure of validation results](#validation-results)
41+
* [Command line](#command-line)
42+
* [Node module](#node-module)
43+
+ [API](#api)
44+
+ [Validation results](#validation-results)
3145
- [Configuration](#configuration)
32-
- [Setup](#setup)
33-
- [Definitions](#definitions)
34-
- [Specs](#specs)
35-
- [Categories](#categories)
36-
- [Rules](#rules)
37-
- [Statuses](#statuses)
38-
- [Configuration file](#configuration-file)
39-
- [Default mode](#default-mode)
40-
- [Default values](#default-values)
41-
- [Troubleshooting](#troubleshooting)
42-
- [Installing with NPM through a proxy](#installing-with-npm-through-a-proxy)
43-
- [Migration Guide](#migration-guide)
46+
* [Setup](#setup)
47+
* [Definitions](#definitions)
48+
+ [Specs](#specs)
49+
+ [Categories](#categories)
50+
+ [Rules](#rules)
51+
+ [Statuses](#statuses)
52+
+ [Configuration Options](#configuration-options)
53+
* [Configuration file](#configuration-file)
54+
* [Default mode](#default-mode)
55+
+ [Default values](#default-values)
56+
* [Spectral configuration](#spectral-configuration)
57+
+ [Changing rule severity](#changing-rule-severity)
58+
+ [Custom rules](#custom-rules)
59+
- [Warnings Limit](#warnings-limit)
60+
- [Turning off `update-notifier`](#turning-off-update-notifier)
4461
- [License](#license)
4562

63+
<!-- tocstop -->
64+
4665
## Installation
4766

4867
### Install with NPM (recommended)
@@ -77,7 +96,7 @@ Once pulled, the container can be run directly, but mount a volume containing th
7796
### Command line
7897
`lint-openapi [options] [command] [<files>]`
7998

80-
#### [options]
99+
##### [options]
81100
- -s (--report_statistics) : Print a simple report at the end of the output showing the frequency, in percentage, of each error/warning.
82101
- -e (--errors_only) : Only print the errors, ignore the warnings.
83102
- -d (--default_mode) : This option turns off [configuration](#configuration) and runs the validator in [default mode](#default-mode).
@@ -91,17 +110,17 @@ Once pulled, the container can be run directly, but mount a volume containing th
91110

92111
_These options only apply to running the validator on a file, not to any commands._
93112

94-
#### [command]
113+
##### [command]
95114
`$ lint-openapi init`
96115
- init : The `init` command initializes a `.validaterc` file, used to [configure](#configuration) the validator. It can also be used to reset the configurable rules to their default values.
97116

98-
#### [command]
117+
##### [command]
99118
`$ lint-openapi migrate`
100119
- migrate : The `migrate` command migrates a `.validaterc` file from the legacy format to the current format, retaining all custom rules. The new format is required - this command provides an option to keep custom rules without manually updating the file or initializing a new configuration file with all rules set to the defaults using `lint-openapi init`.
101120

102121
_None of the above options pertain to these commands._
103122

104-
#### \<files>
123+
##### \<files>
105124
- The OpenAPI document(s) to be validated. All files must be a valid JSON or YAML (only .json, .yml, and .yaml file extensions are supported).
106125
- Multiple, space-separated files can be passed in and each will be validated. This includes support for globs (e.g. `lint-openapi files/*` will run the validator on all files in `files/`)
107126

@@ -160,6 +179,11 @@ Specific validation "rules" can be turned off, or configured to trigger an error
160179
Some validations can be configured even further, such as switching the case convention to validate against for parameter names.
161180
Additionally, certain files can be ignored by the validator. Any glob placed in a file called `.validateignore` will always be ignored by the validator at runtime. This is set up like a `.gitignore` or a `.eslintignore` file.
162181

182+
The validator also employs the [`Spectral`](https://github.com/stoplightio/spectral) validation/linting engine to detect certain issues in the API document.
183+
Spectral rules can also be configured to trigger an error, warning, info, or hint message in the validator output with the `.spectral.yaml` configuration file.
184+
Spectral further supports the creation of custom rules using a simple but powerful yaml syntax or custom Javascript functions.
185+
See the [Spectral configuration](#spectral-configuration) section for more details.
186+
163187
### Setup
164188
To set up the configuration capability, simply run the command `lint-openapi init`.
165189
This will create (or overwrite) a `.validaterc` file with all rules set to their [default value](#default-values). This command does not create a `.validateignore`. That file must be created manually. These rules can then be changed to configure the validator. Continue reading for more details.
@@ -464,6 +488,74 @@ The default values for each rule are described below.
464488
| duplicate_sibling_description | warning |
465489
| incorrect_ref_pattern | warning |
466490

491+
### Spectral configuration
492+
493+
Currently the validator configures Spectral to check the following rules from its
494+
[“oas" ruleset](https://meta.stoplight.io/docs/spectral/docs/reference/openapi-rules.md):
495+
```
496+
no-eval-in-markdown: true
497+
no-script-tags-in-markdown: true
498+
openapi-tags: true
499+
operation-description: true
500+
operation-tags: true
501+
operation-tag-defined: true
502+
path-keys-no-trailing-slash: true
503+
typed-enum: true
504+
oas2-api-host: true
505+
oas2-api-schemes: true
506+
oas2-host-trailing-slash: true
507+
oas2-valid-example: true
508+
oas2-valid-definition-example: true
509+
oas2-anyOf: true
510+
oas2-oneOf: true
511+
oas3-api-servers: true
512+
oas3-examples-value-or-externalValue: true
513+
oas3-server-trailing-slash: true
514+
oas3-valid-example: true
515+
oas3-valid-schema-example: true
516+
```
517+
518+
This ruleset has the alias `ibm:oas`, and you can "extend" this ruleset or specify your own custom ruleset
519+
with a [Spectral ruleset file](https://meta.stoplight.io/docs/spectral/docs/getting-started/3-rulesets.md).
520+
Note that all of the rules in the `spectral:oas` ruleset are defined in `ibm:oas` but only the rules listed above are enabled by default.
521+
522+
You can provide a Spectral ruleset file to the IBM OpenAPI validator in a file named `.spectral.yaml`
523+
in the current directory or with the `--ruleset` command line option of the validator.
524+
525+
#### Changing rule severity
526+
527+
Any rule in the `ibm:oas` ruleset can be configured to trigger an error, warning, info, or hint message in the validator output.
528+
For example, to configure the `openapi-tags` rule to trigger an `info` message instead of a `warning`, specify the following in your Spectral ruleset file:
529+
```
530+
extends: ibm:oas
531+
rules:
532+
openapi-tags: warn
533+
```
534+
535+
To completely disable a rule, use the severity of `off`.
536+
For example, to disable the `operation-tags` rule, specify the following in your Spectral ruleset file:
537+
```
538+
extends: ibm:oas
539+
rules:
540+
operation-tags: off
541+
```
542+
543+
Since the `ibm:oas` ruleset includes all the rules in `spectral:oas`, you can also enable rules from that ruleset that are disabled by default in `ibm:oas`.
544+
For example, to enable the `info-contact` rule with it's default severity (`warning`), specify the following in your Spectral ruleset file:
545+
```
546+
extends: ibm:oas
547+
rules:
548+
info-contact: true
549+
```
550+
551+
You could also set the severity of `info-contact` explicitly to `error`, `warn`, `info`, or `hint`.
552+
553+
#### Custom rules
554+
555+
You can also specify custom rules in the Spectral ruleset file.
556+
Custom rules can be specified using a simple but powerful yaml syntax or with custom Javascript functions.
557+
See the Spectral documentation for detailed documentation on [Spectral custom rules](https://meta.stoplight.io/docs/spectral/docs/guides/4-custom-rulesets.md).
558+
467559
## Warnings Limit
468560

469561
You may impose a warning limit on your API definitions. If the number of warnings issued exceeds the warning limit, the **exit code will be set to 1**. If the Validator is part of your CI build, this will cause the build to fail.

0 commit comments

Comments
 (0)