Skip to content

Commit 8c67e80

Browse files
docs(repo): update rulesets docs (#2310)
1 parent 602dfd4 commit 8c67e80

File tree

9 files changed

+741
-710
lines changed

9 files changed

+741
-710
lines changed

docs/getting-started/3-rulesets.md

Lines changed: 34 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,60 @@
1-
# Rulesets
1+
# Create a Ruleset
22

3-
Rulesets are collections of rules written in JSON, YAML, or [JavaScript](../guides/4-custom-rulesets.md#alternative-js-ruleset-format), which can be used to power powerful linting of other JSON or YAML files. Meta, we know! 😎
3+
Rulesets are collections of rules written in JSON, YAML, or [JavaScript](../guides/4-custom-rulesets.md#alternative-js-ruleset-format), which can be used to power powerful linting of other JSON or YAML files, such as OpenAPI or AsyncAPI descriptions. Meta, we know! 😎
44

5-
These rules are taking parameters, and calling functions on certain parts of another YAML or JSON object being linted.
5+
Ruleset files are often named `.spectral.yaml`, but that's not a requirement.
66

7-
## Anatomy of a Ruleset
7+
Rules take certain parameters and then call functions on parts of another YAML or JSON object being linted.
88

9-
A ruleset is a JSON, YAML, or JavaScript file ([often the file will be called `.spectral.yaml`](../guides/2-cli.md#using-a-ruleset-file)), and there are two main parts.
9+
## Extend an Existing Ruleset
1010

11-
### Rules
11+
The fastest way to create a ruleset is to use the `extends` property to leverage an existing ruleset.
1212

13-
Rules might look a bit like this:
13+
Spectral comes with two built-in rulesets:
1414

15-
```yaml
16-
rules:
17-
paths-kebab-case:
18-
description: Paths should be kebab-case.
19-
message: "{{property}} should be kebab-case (lower-case and separated with hyphens)"
20-
severity: warn
21-
given: $.paths[*]~
22-
then:
23-
function: pattern
24-
functionOptions:
25-
match: "^(\/|[a-z0-9-.]+|{[a-zA-Z0-9_]+})+$"
26-
```
27-
28-
Spectral has [built-in functions](../reference/functions.md) such as `truthy` or `pattern`, which can be used to power rules.
29-
30-
Rules then target certain chunks of the JSON/YAML with the `given` keyword, which is a [JSONPath](http://jsonpath.com/) (actually, we use [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus)).
31-
32-
The example above adds a single rule that looks at the root level `tags` object's children to make sure they all have a `description` property.
33-
34-
### JSONPath Plus
35-
36-
As mentioned, spectral is using JSONPath Plus which expands on the original JSONPath specification to add some additional operators and makes explicit some behaviors the original did not spell out.
37-
38-
Here are some convenient **additions or elaborations**:
39-
40-
- `^` for grabbing the **parent** of a matching item
41-
- `~` for grabbing **property names** of matching items (as array)
42-
- **Type selectors** for obtaining:
43-
- Basic JSON types: `@null()`, `@boolean()`, `@number()`, `@string()`, `@array()`, `@object()`
44-
- `@integer()`
45-
- The compound type `@scalar()` (which also accepts `undefined` and
46-
non-finite numbers when querying JavaScript objects as well as all of the basic non-object/non-function types)
47-
- `@other()` usable in conjunction with a user-defined `otherTypeCallback`
48-
- Non-JSON types that can nevertheless be used when querying
49-
non-JSON JavaScript objects (`@undefined()`, `@function()`, `@nonFinite()`)
50-
- `@path`/`@parent`/`@property`/`@parentProperty`/`@root` **shorthand selectors** within filters
51-
- **Escaping**
52-
- `` ` `` for escaping remaining sequence
53-
- `@['...']`/`?@['...']` syntax for escaping special characters within
54-
property names in filters
55-
- Documents `$..` (**getting all parent components**)
56-
57-
### Extending Rulesets
58-
59-
Rulesets can extend other rulesets using the `extends` property, allowing you to pull in other rulesets.
60-
61-
```yaml
62-
extends: spectral:oas
63-
```
15+
- `spectral:oas` - [OpenAPI v2/v3 rules](./4-openapi.md)
16+
- `spectral:asyncapi` - [AsyncAPI v2 rules](./5-asyncapi.md)
6417

65-
Extends can reference any [distributed ruleset](../guides/7-sharing-rulesets.md). It can be a single string, or an array of strings, and can contain either local file paths, URLs, or even npm modules.
18+
To create a ruleset that extends both rulesets, open your terminal and run:
6619

67-
```yaml
68-
extends:
69-
- ./config/spectral.json
70-
- https://example.org/api/style.yaml
71-
- some-npm-module # note that this would be treated as any other npm package, therefore it has to be placed under node_modules and have a valid package.json.
20+
```bash
21+
echo 'extends: ["spectral:oas", "spectral:asyncapi"]' > .spectral.yaml
7222
```
7323

74-
The `extends` keyword can be combined with extra rules in order to extend and override rulesets. Learn more about that in [custom rulesets](../guides/4-custom-rulesets.md).
75-
76-
### Formats
77-
78-
Formats are an optional way to specify which API description formats a rule, or ruleset, is applicable to. Currently Spectral supports these formats:
79-
80-
- `aas2` (AsyncAPI v2.x)
81-
- `aas2_0` (AsyncAPI v2.0.0)
82-
- `aas2_1` (AsyncAPI v2.1.0)
83-
- `aas2_2` (AsyncAPI v2.2.0)
84-
- `aas2_3` (AsyncAPI v2.3.0)
85-
- `aas2_4` (AsyncAPI v2.4.0)
86-
- `aas2_5` (AsyncAPI v2.5.0)
87-
- `oas2` (OpenAPI v2.0)
88-
- `oas3` (OpenAPI v3.x)
89-
- `oas3_0` (OpenAPI v3.0.x)
90-
- `oas3_1` (OpenAPI v3.1.x)
91-
- `json-schema` (`$schema` says this is some JSON Schema draft)
92-
- `json-schema-loose` (looks like JSON Schema, but no `$schema` found)
93-
- `json-schema-draft4` (`$schema` says this is JSON Schema Draft 04)
94-
- `json-schema-draft6` (`$schema` says this is JSON Schema Draft 06)
95-
- `json-schema-draft7` (`$schema` says this is JSON Schema Draft 07)
96-
- `json-schema-2019-09` (`$schema` says this is JSON Schema 2019-09)
97-
- `json-schema-2020-12` (`$schema` says this is JSON Schema 2020-12)
98-
99-
Specifying the format is optional, so you can completely ignore this if all the rules you are writing apply to any document you lint, or if you have specific rulesets for different formats. If you'd like to use one ruleset for multiple formats, the `formats` key is here to help.
24+
The newly created ruleset file can then be used to lint any OpenAPI v2/v3 or AsyncAPI descriptions using the `spectral lint` command:
10025

101-
```yaml
102-
rules:
103-
oas3-api-servers:
104-
description: "OpenAPI `servers` must be present and non-empty array."
105-
formats: ["oas3"]
106-
given: "$"
107-
then:
108-
field: servers
109-
function: schema
110-
functionOptions:
111-
schema:
112-
items:
113-
type: object
114-
minItems: 1
115-
type: array
26+
```bash
27+
spectral lint myapifile.yaml
11628
```
11729

118-
Specifying the format is optional, so you can completely ignore this if all the rules you are writing apply to any document you lint, or if you have specific rulesets for different formats.
30+
## Write Your First Rule
11931

120-
Formats can be specified at the ruleset level:
32+
Here's what a ruleset with a single rule might look like:
12133

12234
```yaml
123-
formats: ["oas3"]
12435
rules:
125-
oas3-api-servers:
126-
description: "OpenAPI `servers` must be present and non-empty array."
127-
given: "$"
36+
paths-kebab-case:
37+
description: Paths should be kebab-case.
38+
message: "{{property}} should be kebab-case (lower-case and separated with hyphens)"
39+
severity: warn
40+
given: $.paths[*]~
12841
then:
129-
# ...
42+
function: pattern
43+
functionOptions:
44+
match: "^(\/|[a-z0-9-.]+|{[a-zA-Z0-9_]+})+$"
13045
```
13146
132-
Now all the rules in this ruleset will only be applied if the specified format is detected.
133-
134-
If you'd like to use one ruleset for multiple formats but some rules only apply to one format, you can place the `formats` keyword at the rule level instead:
135-
136-
```yaml
137-
rules:
138-
oas3-api-servers:
139-
description: "OpenAPI `servers` must be present and non-empty array."
140-
formats: ["oas3"]
141-
given: "$"
142-
then:
143-
# ...
144-
oas2-hosts:
145-
description: "OpenAPI `servers` must be present and non-empty array."
146-
formats: ["oas2"]
147-
given: "$"
148-
then:
149-
# ...
150-
```
47+
The example above is a rule that can be used to validate an OpenAPI description. It will look at all the `paths` properties to make sure they are kebab-case (lower-case and separated with hyphens).
15148

152-
Custom formats can be registered via the [JS API](../guides/3-javascript.md), but the [CLI](../guides/2-cli.md) is limited to using the predefined formats.
49+
Breaking down each part of the rule:
15350

154-
## Core Rulesets
51+
- `description` and `message` help users quickly understand what the goal of the rule is
52+
- `severity` help define the importance of following the rule
53+
- The `given` keyword tells Spectral what part of the JSON or YAML file to target by using [JSONPath](http://jsonpath.com/) (Spectral uses [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus)).
54+
- The `then` property includes the `function` type and options that tells Spectral how to apply the function to the JSON or YAML file, and make sure that the rule is being followed or not. Spectral has a set of [built-in functions](../reference/functions.md) such as `truthy` or `pattern`, which can be used to power rules.
15555

156-
Spectral comes with two rulesets included:
56+
## Next Steps
15757

158-
- `spectral:oas` - [OpenAPI v2/v3 rules](./4-openapi.md)
159-
- `spectral:asyncapi` - [AsyncAPI v2 rules](./5-asyncapi.md)
58+
For more information about creating Rulesets and Rules, see [Custom Rulesets](../guides/4-custom-rulesets.md).
16059

161-
You can also make your own: read more about [Custom Rulesets](../guides/4-custom-rulesets.md).
60+
For more examples of existing rulesets you can use, see [Real-World Rulesets](../../README.md#-real-world-rulesets).

0 commit comments

Comments
 (0)