Skip to content

Commit 9accd31

Browse files
feat(rulesets): validate channel servers, server securities and operation securities (stoplightio#2122)
1 parent a31d34c commit 9accd31

File tree

7 files changed

+1058
-0
lines changed

7 files changed

+1058
-0
lines changed

docs/reference/asyncapi-rules.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ All channel parameters should be defined in the `parameters` object of the chann
3030

3131
**Recommended:** Yes
3232

33+
### asyncapi-channel-servers
34+
35+
Channel servers must be defined in the `servers` object.
36+
37+
**Bad Example**
38+
39+
```yaml
40+
asyncapi: "2.0.0"
41+
info:
42+
title: Awesome API
43+
description: A very well defined API
44+
version: "1.0"
45+
servers:
46+
production:
47+
url: "stoplight.io"
48+
protocol: "https"
49+
channels:
50+
hello:
51+
servers:
52+
- development
53+
```
54+
55+
**Good Example**
56+
57+
```yaml
58+
asyncapi: "2.0.0"
59+
info:
60+
title: Awesome API
61+
description: A very well defined API
62+
version: "1.0"
63+
servers:
64+
production:
65+
url: "stoplight.io"
66+
protocol: "https"
67+
channels:
68+
hello:
69+
servers:
70+
- production
71+
```
72+
73+
**Recommended:** Yes
74+
3375
### asyncapi-headers-schema-type-object
3476
3577
The schema definition of the application headers must be of type “object”.
@@ -219,6 +261,38 @@ This operation ID is essentially a reference for the operation. Tools may use it
219261

220262
**Recommended:** Yes
221263

264+
### asyncapi-operation-security
265+
266+
Operation `security` values must match a scheme defined in the `components.securitySchemes` object. It also checks if there are `oauth2` scopes that have been defined for the given security.
267+
268+
**Recommended:** Yes
269+
270+
**Good Example**
271+
272+
```yaml
273+
channels:
274+
"user/signup":
275+
publish:
276+
security:
277+
- petstore_auth: []
278+
components:
279+
securitySchemes:
280+
petstore_auth: ...
281+
```
282+
283+
**Bad Example**
284+
285+
```yaml
286+
channels:
287+
"user/signup":
288+
publish:
289+
security:
290+
- not_defined: []
291+
components:
292+
securitySchemes:
293+
petstore_auth: ...
294+
```
295+
222296
### asyncapi-parameter-description
223297

224298
Parameter objects should have a `description`.
@@ -369,6 +443,38 @@ Server URL should not point at example.com.
369443

370444
**Recommended:** No
371445

446+
### asyncapi-server-security
447+
448+
Server `security` values must match a scheme defined in the `components.securitySchemes` object. It also checks if there are `oauth2` scopes that have been defined for the given security.
449+
450+
**Recommended:** Yes
451+
452+
**Good Example**
453+
454+
```yaml
455+
servers:
456+
production:
457+
url: test.mosquitto.org
458+
security:
459+
- petstore_auth: []
460+
components:
461+
securitySchemes:
462+
petstore_auth: ...
463+
```
464+
465+
**Bad Example**
466+
467+
```yaml
468+
servers:
469+
production:
470+
url: test.mosquitto.org
471+
security:
472+
- not_defined: []
473+
components:
474+
securitySchemes:
475+
petstore_auth: ...
476+
```
477+
372478
### asyncapi-server-variables
373479

374480
All server URL variables should be defined in the `variables` object of the server. They should also not contain redundant variables that do not exist in the server address.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from './__helpers__/tester';
3+
4+
testRule('asyncapi-channel-servers', [
5+
{
6+
name: 'valid case',
7+
document: {
8+
asyncapi: '2.2.0',
9+
servers: {
10+
development: {},
11+
production: {},
12+
},
13+
channels: {
14+
channel: {
15+
servers: ['development'],
16+
},
17+
},
18+
},
19+
errors: [],
20+
},
21+
22+
{
23+
name: 'valid case - without defined servers',
24+
document: {
25+
asyncapi: '2.2.0',
26+
servers: {
27+
development: {},
28+
production: {},
29+
},
30+
channels: {
31+
channel: {},
32+
},
33+
},
34+
errors: [],
35+
},
36+
37+
{
38+
name: 'valid case - without defined servers in the root',
39+
document: {
40+
asyncapi: '2.2.0',
41+
channels: {
42+
channel: {},
43+
},
44+
},
45+
errors: [],
46+
},
47+
48+
{
49+
name: 'valid case - without defined channels in the root',
50+
document: {
51+
asyncapi: '2.2.0',
52+
servers: {
53+
development: {},
54+
production: {},
55+
},
56+
},
57+
errors: [],
58+
},
59+
60+
{
61+
name: 'valid case - with empty array',
62+
document: {
63+
asyncapi: '2.2.0',
64+
servers: {
65+
development: {},
66+
production: {},
67+
},
68+
channels: {
69+
channel: {
70+
servers: [],
71+
},
72+
},
73+
},
74+
errors: [],
75+
},
76+
77+
{
78+
name: 'invalid case',
79+
document: {
80+
asyncapi: '2.2.0',
81+
servers: {
82+
development: {},
83+
production: {},
84+
},
85+
channels: {
86+
channel: {
87+
servers: ['another-server'],
88+
},
89+
},
90+
},
91+
errors: [
92+
{
93+
message: 'Channel contains server that are not defined on the "servers" object.',
94+
path: ['channels', 'channel', 'servers', '0'],
95+
severity: DiagnosticSeverity.Error,
96+
},
97+
],
98+
},
99+
100+
{
101+
name: 'invalid case - one server is defined, another one not',
102+
document: {
103+
asyncapi: '2.2.0',
104+
servers: {
105+
development: {},
106+
production: {},
107+
},
108+
channels: {
109+
channel: {
110+
servers: ['production', 'another-server'],
111+
},
112+
},
113+
},
114+
errors: [
115+
{
116+
message: 'Channel contains server that are not defined on the "servers" object.',
117+
path: ['channels', 'channel', 'servers', '1'],
118+
severity: DiagnosticSeverity.Error,
119+
},
120+
],
121+
},
122+
123+
{
124+
name: 'invalid case - without defined servers',
125+
document: {
126+
asyncapi: '2.2.0',
127+
channels: {
128+
channel: {
129+
servers: ['production'],
130+
},
131+
},
132+
},
133+
errors: [
134+
{
135+
message: 'Channel contains server that are not defined on the "servers" object.',
136+
path: ['channels', 'channel', 'servers', '0'],
137+
severity: DiagnosticSeverity.Error,
138+
},
139+
],
140+
},
141+
]);

0 commit comments

Comments
 (0)