diff --git a/docs/reference/asyncapi-rules.md b/docs/reference/asyncapi-rules.md index 9e547a7f73..091f319a26 100644 --- a/docs/reference/asyncapi-rules.md +++ b/docs/reference/asyncapi-rules.md @@ -174,6 +174,12 @@ info: name: MIT ``` +### asyncapi-latest-version + +Checking if the AsyncAPI document is using the latest version. + +**Recommended:** Yes + ### asyncapi-message-examples All `examples` in message object should follow `payload` and `headers` schemas. diff --git a/packages/rulesets/src/asyncapi/__tests__/asyncapi-latest-version.test.ts b/packages/rulesets/src/asyncapi/__tests__/asyncapi-latest-version.test.ts new file mode 100644 index 0000000000..8d48152045 --- /dev/null +++ b/packages/rulesets/src/asyncapi/__tests__/asyncapi-latest-version.test.ts @@ -0,0 +1,27 @@ +import { DiagnosticSeverity } from '@stoplight/types'; +import { latestAsyncApiVersion } from '../functions/asyncApi2DocumentSchema'; +import testRule from './__helpers__/tester'; + +testRule('asyncapi-latest-version', [ + { + name: 'valid case', + document: { + asyncapi: latestAsyncApiVersion, + }, + errors: [], + }, + + { + name: 'invalid case', + document: { + asyncapi: '2.0.0', + }, + errors: [ + { + message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`, + path: ['asyncapi'], + severity: DiagnosticSeverity.Information, + }, + ], + }, +]); diff --git a/packages/rulesets/src/asyncapi/functions/asyncApi2DocumentSchema.ts b/packages/rulesets/src/asyncapi/functions/asyncApi2DocumentSchema.ts index 4ac8381d01..fe2085612b 100644 --- a/packages/rulesets/src/asyncapi/functions/asyncApi2DocumentSchema.ts +++ b/packages/rulesets/src/asyncapi/functions/asyncApi2DocumentSchema.ts @@ -12,6 +12,9 @@ import * as asyncAPI2_2_0Schema from '@asyncapi/specs/schemas/2.2.0.json'; import * as asyncAPI2_3_0Schema from '@asyncapi/specs/schemas/2.3.0.json'; import * as asyncAPI2_4_0Schema from '@asyncapi/specs/schemas/2.4.0.json'; +export const asyncApiSpecVersions = ['2.0.0', '2.1.0', '2.2.0', '2.3.0', '2.4.0']; +export const latestAsyncApiVersion = asyncApiSpecVersions[asyncApiSpecVersions.length - 1]; + function shouldIgnoreError(error: ErrorObject): boolean { return ( // oneOf is a fairly error as we have 2 options to choose from for most of the time. diff --git a/packages/rulesets/src/asyncapi/index.ts b/packages/rulesets/src/asyncapi/index.ts index e7d387bc44..d336ca2977 100644 --- a/packages/rulesets/src/asyncapi/index.ts +++ b/packages/rulesets/src/asyncapi/index.ts @@ -10,7 +10,7 @@ import { import asyncApi2ChannelParameters from './functions/asyncApi2ChannelParameters'; import asyncApi2ChannelServers from './functions/asyncApi2ChannelServers'; -import asyncApi2DocumentSchema from './functions/asyncApi2DocumentSchema'; +import asyncApi2DocumentSchema, { latestAsyncApiVersion } from './functions/asyncApi2DocumentSchema'; import asyncApi2MessageExamplesValidation from './functions/asyncApi2MessageExamplesValidation'; import asyncApi2MessageIdUniqueness from './functions/asyncApi2MessageIdUniqueness'; import asyncApi2OperationIdUniqueness from './functions/asyncApi2OperationIdUniqueness'; @@ -172,6 +172,22 @@ export default { function: truthy, }, }, + 'asyncapi-latest-version': { + description: 'Checking if the AsyncAPI document is using the latest version.', + message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`, + recommended: true, + type: 'style', + severity: 'info', + given: '$.asyncapi', + then: { + function: schema, + functionOptions: { + schema: { + const: latestAsyncApiVersion, + }, + }, + }, + }, 'asyncapi-message-examples': { description: 'Examples of message object should follow by "payload" and "headers" schemas.', message: '{{error}}',