Skip to content

Commit d2186ba

Browse files
committed
feat: create rule asyncapi3-channel-servers for v3 core ruleset
1 parent 606d4b1 commit d2186ba

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@asyncapi/parser": minor
3+
---
4+
5+
feat: create the rule `asyncapi3-channel-servers` for the v3 rule core ruleset

packages/parser/src/ruleset/v2/functions/channelServers.ts renamed to packages/parser/src/ruleset/functions/channelServers.ts

File renamed without changes.

packages/parser/src/ruleset/v2/ruleset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AsyncAPIFormats } from '../formats';
44
import { truthy, pattern } from '@stoplight/spectral-functions';
55

66
import { channelParameters } from './functions/channelParameters';
7-
import { channelServers } from './functions/channelServers';
7+
import { channelServers } from '../functions/channelServers';
88
import { checkId } from './functions/checkId';
99
import { messageExamples } from './functions/messageExamples';
1010
import { asyncApi2MessageExamplesParserRule } from './functions/messageExamples-spectral-rule-v2';

packages/parser/src/ruleset/v3/ruleset.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { AsyncAPIFormats } from '../formats';
44
import { operationMessagesUnambiguity } from './functions/operationMessagesUnambiguity';
55
import { pattern } from '@stoplight/spectral-functions';
6+
import { channelServers } from '../functions/channelServers';
67

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

0 commit comments

Comments
 (0)