Skip to content

Commit 366779f

Browse files
feat(rulesets): add rule to check if the AsyncAPI document is using the latest version (#2282)
1 parent 33e3770 commit 366779f

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

docs/reference/asyncapi-rules.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ info:
174174
name: MIT
175175
```
176176

177+
### asyncapi-latest-version
178+
179+
Checking if the AsyncAPI document is using the latest version.
180+
181+
**Recommended:** Yes
182+
177183
### asyncapi-message-examples
178184

179185
All `examples` in message object should follow `payload` and `headers` schemas.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import { latestAsyncApiVersion } from '../functions/asyncApi2DocumentSchema';
3+
import testRule from './__helpers__/tester';
4+
5+
testRule('asyncapi-latest-version', [
6+
{
7+
name: 'valid case',
8+
document: {
9+
asyncapi: latestAsyncApiVersion,
10+
},
11+
errors: [],
12+
},
13+
14+
{
15+
name: 'invalid case',
16+
document: {
17+
asyncapi: '2.0.0',
18+
},
19+
errors: [
20+
{
21+
message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`,
22+
path: ['asyncapi'],
23+
severity: DiagnosticSeverity.Information,
24+
},
25+
],
26+
},
27+
]);

packages/rulesets/src/asyncapi/functions/asyncApi2DocumentSchema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import * as asyncAPI2_2_0Schema from '@asyncapi/specs/schemas/2.2.0.json';
1212
import * as asyncAPI2_3_0Schema from '@asyncapi/specs/schemas/2.3.0.json';
1313
import * as asyncAPI2_4_0Schema from '@asyncapi/specs/schemas/2.4.0.json';
1414

15+
export const asyncApiSpecVersions = ['2.0.0', '2.1.0', '2.2.0', '2.3.0', '2.4.0'];
16+
export const latestAsyncApiVersion = asyncApiSpecVersions[asyncApiSpecVersions.length - 1];
17+
1518
function shouldIgnoreError(error: ErrorObject): boolean {
1619
return (
1720
// oneOf is a fairly error as we have 2 options to choose from for most of the time.

packages/rulesets/src/asyncapi/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import asyncApi2ChannelParameters from './functions/asyncApi2ChannelParameters';
1212
import asyncApi2ChannelServers from './functions/asyncApi2ChannelServers';
13-
import asyncApi2DocumentSchema from './functions/asyncApi2DocumentSchema';
13+
import asyncApi2DocumentSchema, { latestAsyncApiVersion } from './functions/asyncApi2DocumentSchema';
1414
import asyncApi2MessageExamplesValidation from './functions/asyncApi2MessageExamplesValidation';
1515
import asyncApi2MessageIdUniqueness from './functions/asyncApi2MessageIdUniqueness';
1616
import asyncApi2OperationIdUniqueness from './functions/asyncApi2OperationIdUniqueness';
@@ -172,6 +172,22 @@ export default {
172172
function: truthy,
173173
},
174174
},
175+
'asyncapi-latest-version': {
176+
description: 'Checking if the AsyncAPI document is using the latest version.',
177+
message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`,
178+
recommended: true,
179+
type: 'style',
180+
severity: 'info',
181+
given: '$.asyncapi',
182+
then: {
183+
function: schema,
184+
functionOptions: {
185+
schema: {
186+
const: latestAsyncApiVersion,
187+
},
188+
},
189+
},
190+
},
175191
'asyncapi-message-examples': {
176192
description: 'Examples of message object should follow by "payload" and "headers" schemas.',
177193
message: '{{error}}',

test-harness/scenarios/asyncapi2-streetlights.scenario

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ module.exports = asyncapi;
217217
{bin} lint {document} --ruleset "{asset:ruleset}"
218218
====stdout====
219219
{document}
220-
1:1 warning asyncapi-tags AsyncAPI object must have non-empty "tags" array.
221-
2:6 warning asyncapi-info-contact Info object must have "contact" object. info
222-
45:13 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured.publish
223-
57:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/on.subscribe
224-
68:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/off.subscribe
225-
79:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/dim.subscribe
220+
1:1 warning asyncapi-tags AsyncAPI object must have non-empty "tags" array.
221+
1:11 information asyncapi-latest-version The latest version is not used. You should update to the "2.4.0" version. asyncapi
222+
2:6 warning asyncapi-info-contact Info object must have "contact" object. info
223+
45:13 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured.publish
224+
57:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/on.subscribe
225+
68:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/off.subscribe
226+
79:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/dim.subscribe
226227

227-
6 problems (0 errors, 6 warnings, 0 infos, 0 hints)
228+
7 problems (0 errors, 6 warnings, 1 info, 0 hints)

0 commit comments

Comments
 (0)