From 9c3ddffa801fc5d52fa53d3b5e19452583d39c48 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Sat, 10 Aug 2024 08:42:20 +0000 Subject: [PATCH] chore: Create the rule `asyncapi3-channel-no-query-nor-fragment` for the v3 core ruleset --- packages/parser/src/ruleset/v2/ruleset.ts | 26 +++++----- packages/parser/src/ruleset/v3/ruleset.ts | 15 +++++- ...api3-channel-no-query-nor-fragment.spec.ts | 50 +++++++++++++++++++ 3 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 packages/parser/test/ruleset/rules/v3/asyncapi3-channel-no-query-nor-fragment.spec.ts diff --git a/packages/parser/src/ruleset/v2/ruleset.ts b/packages/parser/src/ruleset/v2/ruleset.ts index 7e80f89f6..f5e0d3274 100644 --- a/packages/parser/src/ruleset/v2/ruleset.ts +++ b/packages/parser/src/ruleset/v2/ruleset.ts @@ -49,19 +49,6 @@ export const v2CoreRuleset = { function: serverVariables, }, }, - 'asyncapi2-channel-no-query-nor-fragment': { - description: 'Channel address should not include query ("?") or fragment ("#") delimiter.', - severity: 'error', - recommended: true, - given: '$.channels', - then: { - field: '@key', - function: pattern, - functionOptions: { - notMatch: '[\\?#]', - }, - }, - }, /** * Channel Object rules @@ -86,6 +73,19 @@ export const v2CoreRuleset = { function: channelServers, }, }, + 'asyncapi2-channel-no-query-nor-fragment': { + description: 'Channel address should not include query ("?") or fragment ("#") delimiter.', + severity: 'error', + recommended: true, + given: '$.channels', + then: { + field: '@key', + function: pattern, + functionOptions: { + notMatch: '[\\?#]', + }, + }, + }, /** * Operation Object rules diff --git a/packages/parser/src/ruleset/v3/ruleset.ts b/packages/parser/src/ruleset/v3/ruleset.ts index 724339e84..62e7ecb51 100644 --- a/packages/parser/src/ruleset/v3/ruleset.ts +++ b/packages/parser/src/ruleset/v3/ruleset.ts @@ -56,6 +56,19 @@ export const v3CoreRuleset = { match: '#\\/servers\\/', // If doesn't match, rule fails. }, }, - } + }, + 'asyncapi3-channel-no-query-nor-fragment': { + description: 'Channel address should not include query ("?") or fragment ("#") delimiter.', + severity: 'error', + recommended: true, + given: '$.channels', + then: { + field: '@key', + function: pattern, + functionOptions: { + notMatch: '[\\?#]', + }, + }, + }, }, }; diff --git a/packages/parser/test/ruleset/rules/v3/asyncapi3-channel-no-query-nor-fragment.spec.ts b/packages/parser/test/ruleset/rules/v3/asyncapi3-channel-no-query-nor-fragment.spec.ts new file mode 100644 index 000000000..f0dd5231f --- /dev/null +++ b/packages/parser/test/ruleset/rules/v3/asyncapi3-channel-no-query-nor-fragment.spec.ts @@ -0,0 +1,50 @@ +import { testRule, DiagnosticSeverity } from '../../tester'; + +testRule('asyncapi3-channel-no-query-nor-fragment', [ + { + name: 'valid case', + document: { + asyncapi: '3.0.0', + channels: { + 'users/{userId}/signedUp': {}, + }, + }, + errors: [], + }, + + { + name: 'channels.{channel} contains a query delimiter', + document: { + asyncapi: '3.0.0', + channels: { + 'users/{userId}/signedUp': {}, + 'users/{userId}/signedOut?byMistake={didFatFingerTheSignOutButton}': {}, + }, + }, + errors: [ + { + message: 'Channel address should not include query ("?") or fragment ("#") delimiter.', + path: ['channels', 'users/{userId}/signedOut?byMistake={didFatFingerTheSignOutButton}'], + severity: DiagnosticSeverity.Error, + }, + ], + }, + + { + name: 'channels.{channel} contains a fragment delimiter', + document: { + asyncapi: '3.0.0', + channels: { + 'users/{userId}/signedUp': {}, + 'users/{userId}/signedOut#onPurpose': {}, + }, + }, + errors: [ + { + message: 'Channel address should not include query ("?") or fragment ("#") delimiter.', + path: ['channels', 'users/{userId}/signedOut#onPurpose'], + severity: DiagnosticSeverity.Error, + }, + ], + }, +]);