Skip to content

Commit 28ecf19

Browse files
authored
chore: lint JSON/YAML/Markdown files (#1360)
1 parent c2fe4a7 commit 28ecf19

24 files changed

+464
-820
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/__fixtures__/**
2+
/test-harness/**/*.yaml

.prettierrc.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,15 @@
44
"trailingComma": "all",
55
"printWidth": 120,
66
"proseWrap": "always",
7-
"endOfLine": "auto"
7+
"endOfLine": "auto",
8+
"overrides": [
9+
{
10+
"files": ["*.md"],
11+
"options": {
12+
"singleQuote": false,
13+
"printWidth": 200,
14+
"proseWrap": "preserve"
15+
}
16+
}
17+
]
818
}

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
![Spectral logo](./docs/img/spectral-banner.png)
22

3-
[![CircleCI](https://img.shields.io/circleci/build/github/stoplightio/spectral/master)](https://circleci.com/gh/stoplightio/spectral)
4-
[![NPM Downloads](https://img.shields.io/npm/dw/@stoplight/spectral?color=blue)](https://www.npmjs.com/package/@stoplight/spectral)
5-
[![Treeware (Trees)](https://img.shields.io/treeware/trees/stoplightio/spectral)](https://plant.treeware.earth/stoplightio/spectral)
3+
[![CircleCI](https://img.shields.io/circleci/build/github/stoplightio/spectral/master)](https://circleci.com/gh/stoplightio/spectral) [![NPM Downloads](https://img.shields.io/npm/dw/@stoplight/spectral?color=blue)](https://www.npmjs.com/package/@stoplight/spectral) [![Treeware (Trees)](https://img.shields.io/treeware/trees/stoplightio/spectral)](https://plant.treeware.earth/stoplightio/spectral)
64

75
A flexible JSON/YAML linter, with out of the box support for OpenAPI v2/v3 and AsyncAPI v2.
86

docs/getting-started/2-installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ validate_open-api:
4848
name: stoplight/spectral
4949
entrypoint: [""]
5050
script:
51-
- spectral lint file.yaml
51+
- spectral lint file.yaml
5252
```
5353
54-
For more details about `entrypoint: [""]` see this issue on GitLab [here](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2692#note_50147081)
54+
For more details about `entrypoint: [""]` see this issue on GitLab [here](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2692#note_50147081)

docs/getting-started/3-rulesets.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Extends can reference any [distributed ruleset](../guides/7-sharing-rulesets.md)
4141

4242
```yaml
4343
extends:
44-
- ./config/spectral.json
45-
- https://example.org/api/style.yaml
46-
- some-npm-module
44+
- ./config/spectral.json
45+
- https://example.org/api/style.yaml
46+
- some-npm-module
4747
```
4848

4949
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).

docs/getting-started/4-openapi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Spectral is a generic linter, but a lot of effort has been put in to making sure
55
Run Spectral against a document without specifying a ruleset will trigger an auto-detect, where Spectral will look to see if `swagger: 2.0` or `openapi: 3.0.x` are in the root of the document. If it finds either of those it will load `spectral:oas`, which is documented in our [Reference > OpenAPI Rules](../reference/openapi-rules.md).
66

77
<!-- theme: info -->
8+
89
> If you would like support for other API description formats like [RAML](https://raml.org/), message formats like [JSON:API](https://jsonapi.org/), etc., we recommend you start building custom but generic rulesets which can be shared with others. We've started putting together some over here on [OpenAPI Contrib](https://github.com/openapi-contrib/style-guides/).

docs/guides/2-cli.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ To have requests made from Spectral be proxied through a server, you'd need to s
6464

6565
`PROXY=<<PROXY_SERVER_ADDRESS>> spectral lint spec.yaml`
6666

67-
## Custom $ref Resolving
67+
## Custom \$ref Resolving
6868

69-
If you want to customize $ref resolving, you can leverage `--resolver` flag and pass a path to the JS file exporting a custom instance of json-ref-resolver Resolver.
69+
If you want to customize \$ref resolving, you can leverage `--resolver` flag and pass a path to the JS file exporting a custom instance of json-ref-resolver Resolver.
7070

7171
### Example
7272

7373
Assuming the filename is called `my-resolver.js` and the content looks as follows, the path should look more or less like `--resolver=./my-resolver.js`.
7474

7575
```js
76-
const { Resolver } = require('@stoplight/json-ref-resolver');
76+
const { Resolver } = require("@stoplight/json-ref-resolver");
7777

7878
module.exports = new Resolver({
7979
resolvers: {
8080
// pass any resolver for protocol you need
81-
}
81+
},
8282
});
8383
```
8484

docs/guides/3-javascript.md

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,38 @@ Assuming it has been installed as a Node module via NPM/Yarn, it can be used to
77
## Linting a YAML String
88

99
```js
10-
const { Spectral, Document, Parsers } = require('@stoplight/spectral');
10+
const { Spectral, Document, Parsers } = require("@stoplight/spectral");
1111

12-
const myOpenApiDocument = new Document(`responses:
12+
const myOpenApiDocument = new Document(
13+
`responses:
1314
'200':
1415
description: ''
1516
schema:
1617
$ref: '#/definitions/error-response'
17-
`, Parsers.Yaml);
18+
`,
19+
Parsers.Yaml,
20+
);
1821

1922
const spectral = new Spectral();
20-
spectral
21-
.run(myOpenApiDocument)
22-
.then(console.log);
23+
spectral.run(myOpenApiDocument).then(console.log);
2324
```
2425

25-
This will run Spectral with no formats, rules or functions, so it's not going to do anything besides $ref resolving.
26+
This will run Spectral with no formats, rules or functions, so it's not going to do anything besides \$ref resolving.
2627
Find out how to add formats, rules and functions below.
2728

2829
## Linting an Object
2930

3031
Instead of passing a string to `Document`, you can pass in JavaScript object, with or without `$ref`'s.
3132

3233
```js
33-
const { Spectral } = require('@stoplight/spectral');
34+
const { Spectral } = require("@stoplight/spectral");
3435

3536
const myOpenApiDocument = {
3637
responses: {
37-
'200': {
38-
description: '',
38+
"200": {
39+
description: "",
3940
schema: {
40-
$ref: '#/definitions/error-response',
41+
$ref: "#/definitions/error-response",
4142
},
4243
},
4344
},
@@ -57,33 +58,25 @@ Assuming your rulesets use the built-in Spectral formats, this can be accomplish
5758
- OpenAPI
5859

5960
```js
60-
const { Spectral, isOpenApiv2, isOpenApiv3 } = require('@stoplight/spectral');
61+
const { Spectral, isOpenApiv2, isOpenApiv3 } = require("@stoplight/spectral");
6162

6263
const spectral = new Spectral();
63-
spectral.registerFormat('oas2', isOpenApiv2);
64-
spectral.registerFormat('oas3', isOpenApiv3);
64+
spectral.registerFormat("oas2", isOpenApiv2);
65+
spectral.registerFormat("oas3", isOpenApiv3);
6566
```
6667

6768
- JSON Schema
6869

6970
```js
70-
const {
71-
Spectral,
72-
isJSONSchema,
73-
isJSONSchemaDraft4,
74-
isJSONSchemaDraft6,
75-
isJSONSchemaDraft7,
76-
isJSONSchemaDraft2019_09,
77-
isJSONSchemaLoose,
78-
} = require('@stoplight/spectral');
71+
const { Spectral, isJSONSchema, isJSONSchemaDraft4, isJSONSchemaDraft6, isJSONSchemaDraft7, isJSONSchemaDraft2019_09, isJSONSchemaLoose } = require("@stoplight/spectral");
7972

8073
const spectral = new Spectral();
81-
spectral.registerFormat('json-schema', isJSONSchema);
82-
spectral.registerFormat('json-schema-loose', isJSONSchemaLoose);
83-
spectral.registerFormat('json-schema-draft4', isJSONSchemaDraft4);
84-
spectral.registerFormat('json-schema-draft6', isJSONSchemaDraft6);
85-
spectral.registerFormat('json-schema-draft7', isJSONSchemaDraft7);
86-
spectral.registerFormat('json-schema-2019-09', isJSONSchemaDraft2019_09);
74+
spectral.registerFormat("json-schema", isJSONSchema);
75+
spectral.registerFormat("json-schema-loose", isJSONSchemaLoose);
76+
spectral.registerFormat("json-schema-draft4", isJSONSchemaDraft4);
77+
spectral.registerFormat("json-schema-draft6", isJSONSchemaDraft6);
78+
spectral.registerFormat("json-schema-draft7", isJSONSchemaDraft7);
79+
spectral.registerFormat("json-schema-2019-09", isJSONSchemaDraft2019_09);
8780
```
8881

8982
Learn more about predefined formats in the [ruleset documentation](../getting-started/3-rulesets.md#formats).
@@ -93,20 +86,21 @@ Learn more about predefined formats in the [ruleset documentation](../getting-st
9386
Spectral comes with some rulesets that are very specific to OpenAPI v2/v3, and they can be loaded using `Spectral.loadRuleset()`.
9487

9588
```js
96-
const { Spectral, isOpenApiv2, isOpenApiv3 } = require('@stoplight/spectral');
89+
const { Spectral, isOpenApiv2, isOpenApiv3 } = require("@stoplight/spectral");
9790

9891
const myOpenApiDocument = `
9992
openapi: 3.0.0
10093
# here goes the rest of document
101-
`
94+
`;
10295

10396
const spectral = new Spectral();
104-
spectral.registerFormat('oas2', isOpenApiv2);
105-
spectral.registerFormat('oas3', isOpenApiv3);
106-
spectral.loadRuleset('spectral:oas')
97+
spectral.registerFormat("oas2", isOpenApiv2);
98+
spectral.registerFormat("oas3", isOpenApiv3);
99+
spectral
100+
.loadRuleset("spectral:oas")
107101
.then(() => spectral.run(myOpenApiDocument))
108102
.then(results => {
109-
console.log('here are the results', results);
103+
console.log("here are the results", results);
110104
});
111105
```
112106

@@ -137,15 +131,14 @@ spectral.loadRuleset(join(__dirname './path/to/my-ruleset.yaml'));
137131
Alternatively, if your ruleset is stored in a plain JSON file that doesn't extend any other rulesets, you can also consider using `setRuleset`, as follows
138132

139133
```js
140-
const { Spectral } = require('@stoplight/spectral');
141-
const ruleset = require('./my-ruleset.json');
134+
const { Spectral } = require("@stoplight/spectral");
135+
const ruleset = require("./my-ruleset.json");
142136

143137
const spectral = new Spectral();
144138
spectral.setRuleset(ruleset);
145-
spectral.run(myOpenApiDocument)
146-
.then(results => {
147-
console.log('here are the results', results);
148-
});
139+
spectral.run(myOpenApiDocument).then(results => {
140+
console.log("here are the results", results);
141+
});
149142
```
150143

151144
## Advanced
@@ -155,101 +148,99 @@ spectral.run(myOpenApiDocument)
155148
Spectral supports two core formats: `oas2` and `oas3`. Using `registerFormat` you can add support for auto-detecting other formats. You might want to do this for a ruleset which is run against multiple major versions of description format like RAML v0.8 and v1.0.
156149

157150
```js
158-
const { Spectral } = require('@stoplight/spectral');
151+
const { Spectral } = require("@stoplight/spectral");
159152

160153
const spectral = new Spectral();
161154

162-
spectral.registerFormat('foo-bar', obj => typeof obj === 'object' && obj !== null && 'foo-bar' in obj);
155+
spectral.registerFormat("foo-bar", obj => typeof obj === "object" && obj !== null && "foo-bar" in obj);
163156

164157
spectral.setRuleset({
165158
functions: {},
166159
rules: {
167160
rule1: {
168-
given: '$.x',
169-
formats: ['foo-bar'],
170-
severity: 'error',
161+
given: "$.x",
162+
formats: ["foo-bar"],
163+
severity: "error",
171164
then: {
172-
function: 'truthy',
165+
function: "truthy",
173166
},
174167
},
175168
},
176169
});
177170

178171
spectral
179172
.run({
180-
'foo-bar': true,
181-
x: false
173+
"foo-bar": true,
174+
x: false,
182175
})
183176
.then(result => {
184177
expect(result).toEqual([
185178
expect.objectContaining({
186-
code: 'rule1',
187-
}),
188-
]);
179+
code: "rule1",
180+
}),
181+
]);
189182
});
190183
```
191184

192185
Alternatively you may lookup for certain format by optional `source`, which could be passed in `run` options.
193186

194187
```js
195-
const { Document, Spectral } = require('@stoplight/spectral');
188+
const { Document, Spectral } = require("@stoplight/spectral");
196189

197190
const spectral = new Spectral();
198191

199-
spectral.registerFormat('foo-bar', (_, source) => source === '/foo/bar');
192+
spectral.registerFormat("foo-bar", (_, source) => source === "/foo/bar");
200193

201194
spectral.setRuleset({
202195
functions: {},
203196
rules: {
204197
rule1: {
205-
given: '$.x',
206-
formats: ['foo-bar'],
207-
severity: 'error',
198+
given: "$.x",
199+
formats: ["foo-bar"],
200+
severity: "error",
208201
then: {
209-
function: 'truthy',
202+
function: "truthy",
210203
},
211204
},
212205
},
213206
});
214207

215-
spectral
216-
.run(new Document(`foo-bar: true\nx: false`, Parsers.Yaml, '/foo/bar'))
217-
.then(result => {
218-
expect(result).toEqual([
219-
expect.objectContaining({
220-
code: 'rule1',
221-
}),
222-
]);
223-
});
208+
spectral.run(new Document(`foo-bar: true\nx: false`, Parsers.Yaml, "/foo/bar")).then(result => {
209+
expect(result).toEqual([
210+
expect.objectContaining({
211+
code: "rule1",
212+
}),
213+
]);
214+
});
224215
```
225216

226217
### Using a Proxy
227218

228219
Spectral supports HTTP(S) proxies when fetching remote schemas and rulesets.
229220

230221
```js
231-
const { Spectral } = require('@stoplight/spectral');
222+
const { Spectral } = require("@stoplight/spectral");
232223

233-
const spectral = new Spectral({ proxyUri: 'http://my-proxy:3000' });
234-
spectral.loadRuleset('https://example.org/my-rules')
224+
const spectral = new Spectral({ proxyUri: "http://my-proxy:3000" });
225+
spectral.loadRuleset("https://example.org/my-rules");
235226

236227
// lint as usual - $refs and rules will be requested using the proxy
237228
```
238229

239230
### Using a Custom Resolver
240231

241-
Spectral lets you provide any custom $ref resolver. By default, http(s) and file protocols are resolved, relatively to
232+
Spectral lets you provide any custom \$ref resolver. By default, http(s) and file protocols are resolved, relatively to
242233
the document Spectral lints against. If you'd like support any additional protocol or adjust the resolution, you are
243234
absolutely fine to do it. In order to achieve that, you need to create a custom json-ref-resolver instance.
244235

245236
You can find more information about how to create custom resolvers in
246237
the [@stoplight/json-ref-resolver](https://github.com/stoplightio/json-ref-resolver) repository.
247238

248239
```js
249-
const path = require('path');
250-
const fs = require('fs');
251-
const { Spectral } = require('@stoplight/spectral');
252-
const { Resolver } = require('@stoplight/json-ref-resolver');
240+
const path = require("path");
241+
const fs = require("fs");
242+
const { Spectral } = require("@stoplight/spectral");
243+
const { Resolver } = require("@stoplight/json-ref-resolver");
253244

254245
const customFileResolver = new Resolver({
255246
resolvers: {
@@ -258,17 +249,17 @@ const customFileResolver = new Resolver({
258249
return new Promise((resolve, reject) => {
259250
const basePath = process.cwd();
260251
const refPath = ref.path();
261-
fs.readFile(path.join(basePath, refPath), 'utf8', (err, data) => {
252+
fs.readFile(path.join(basePath, refPath), "utf8", (err, data) => {
262253
if (err) {
263254
reject(err);
264255
} else {
265256
resolve(data);
266257
}
267258
});
268259
});
269-
}
270-
}
271-
}
260+
},
261+
},
262+
},
272263
});
273264

274265
const spectral = new Spectral({ resolver: customFileResolver });

0 commit comments

Comments
 (0)