Skip to content

Commit

Permalink
feat(rulesets): support AsyncAPI 2.6.0 (#2391)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored and P0lip committed Feb 3, 2023
1 parent 27552a7 commit 8315508
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 256 deletions.
4 changes: 2 additions & 2 deletions packages/rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"release": "semantic-release -e semantic-release-monorepo"
},
"dependencies": {
"@asyncapi/specs": "^3.2.0",
"@asyncapi/specs": "^4.1.0",
"@stoplight/better-ajv-errors": "1.0.3",
"@stoplight/json": "^3.17.0",
"@stoplight/spectral-core": "^1.8.1",
"@stoplight/spectral-formats": "^1.4.0",
"@stoplight/spectral-formats": "^1.5.0",
"@stoplight/spectral-functions": "^1.5.1",
"@stoplight/spectral-runtime": "^1.1.1",
"@stoplight/types": "^13.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,116 @@ describe('asyncApi2DocumentSchema', () => {
});
});

describe('given AsyncAPI 2.4.0 document', () => {
test('validate messageId on message', async () => {
expect(
await s.run({
asyncapi: '2.4.0',
info: {
title: 'Signup service example (internal)',
version: '0.1.0',
},
channels: {
'/user/signedup': {
subscribe: {
message: {
messageId: 'messageId',
payload: {
type: 'object',
properties: {
email: {
type: 'string',
format: 'email',
},
},
},
},
},
},
},
}),
).toEqual([]);
});
});

describe('given AsyncAPI 2.5.0 document', () => {
test('validate tags on server', async () => {
expect(
await s.run({
asyncapi: '2.5.0',
info: {
title: 'Signup service example (internal)',
version: '0.1.0',
},
servers: {
development: {
url: 'https://some-server.com/example',
protocol: 'kafka',
tags: [
{
name: 'env:production',
},
{
name: 'e-commerce',
},
],
},
},
channels: {
'/user/signedup': {
subscribe: {
message: {
messageId: 'messageId',
payload: {
type: 'object',
properties: {
email: {
type: 'string',
format: 'email',
},
},
},
},
},
},
},
}),
).toEqual([]);
});
});

describe('given AsyncAPI 2.6.0 document', () => {
test('validate valid spec', async () => {
expect(
await s.run({
asyncapi: '2.6.0',
info: {
title: 'Signup service example (internal)',
version: '0.1.0',
},
channels: {
'/user/signedup': {
subscribe: {
message: {
messageId: 'messageId',
payload: {
type: 'object',
properties: {
email: {
type: 'string',
format: 'email',
},
},
},
},
},
},
},
}),
).toEqual([]);
});
});

describe('prepareResults', () => {
test('given oneOf error one of which is required $ref property missing, picks only one error', () => {
const errors: ErrorObject[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRulesetFunction } from '@stoplight/spectral-core';
import { schema as schemaFn } from '@stoplight/spectral-functions';
import { aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5 } from '@stoplight/spectral-formats';
import { aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5, aas2_6 } from '@stoplight/spectral-formats';

import { getCopyOfSchema } from './utils/specs';

Expand Down Expand Up @@ -92,6 +92,8 @@ function getSerializedSchema(version: AsyncAPISpecVersion): Record<string, unkno

function getSchema(formats: Set<Format>): Record<string, any> | void {
switch (true) {
case formats.has(aas2_6):
return getSerializedSchema('2.6.0');
case formats.has(aas2_5):
return getSerializedSchema('2.5.0');
case formats.has(aas2_4):
Expand Down
17 changes: 1 addition & 16 deletions packages/rulesets/src/asyncapi/functions/utils/specs.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
// import only 2.X.X AsyncAPI JSON Schemas for better treeshaking
import * as asyncAPI2_0_0Schema from '@asyncapi/specs/schemas/2.0.0.json';
import * as asyncAPI2_1_0Schema from '@asyncapi/specs/schemas/2.1.0.json';
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';
import * as asyncAPI2_5_0Schema from '@asyncapi/specs/schemas/2.5.0.json';
import specs from '@asyncapi/specs';

export type AsyncAPISpecVersion = keyof typeof specs;

export const specs = {
'2.0.0': asyncAPI2_0_0Schema,
'2.1.0': asyncAPI2_1_0Schema,
'2.2.0': asyncAPI2_2_0Schema,
'2.3.0': asyncAPI2_3_0Schema,
'2.4.0': asyncAPI2_4_0Schema,
'2.5.0': asyncAPI2_5_0Schema,
};

const versions = Object.keys(specs);
export const latestVersion = versions[versions.length - 1];

Expand Down
4 changes: 2 additions & 2 deletions packages/rulesets/src/asyncapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5 } from '@stoplight/spectral-formats';
import { aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5, aas2_6 } from '@stoplight/spectral-formats';
import {
truthy,
pattern,
Expand All @@ -23,7 +23,7 @@ import { latestVersion } from './functions/utils/specs';

export default {
documentationUrl: 'https://meta.stoplight.io/docs/spectral/docs/reference/asyncapi-rules.md',
formats: [aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5],
formats: [aas2_0, aas2_1, aas2_2, aas2_3, aas2_4, aas2_5, aas2_6],
rules: {
'asyncapi-channel-no-empty-parameter': {
description: 'Channel path must not have empty parameter substitution pattern.',
Expand Down
Loading

0 comments on commit 8315508

Please sign in to comment.