Skip to content

Commit

Permalink
feat(rulesets): add rule to check if the AsyncAPI document is using t…
Browse files Browse the repository at this point in the history
…he latest version.
  • Loading branch information
magicmatatjahu committed Sep 17, 2022
1 parent 8bbf7bb commit ebbf782
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/reference/asyncapi-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
},
],
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion packages/rulesets/src/asyncapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}}',
Expand Down

0 comments on commit ebbf782

Please sign in to comment.