-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Spectral rule to validate operation messages
- Loading branch information
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
import { SchemaDefinition } from '@stoplight/spectral-core/dist/ruleset/function'; | ||
|
||
const referenceSchema: SchemaDefinition = { | ||
type: 'object', | ||
properties: { | ||
$ref: { | ||
type: 'string', | ||
format: 'uri-reference' | ||
}, | ||
}, | ||
}; | ||
|
||
export const operationMessagesUnambiguity = createRulesetFunction<{ channel?: {'$ref': string}; messages?: [{'$ref': string}] }, null>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
channel: referenceSchema, | ||
messages: { | ||
type: 'array', | ||
items: referenceSchema, | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
(targetVal, _, ctx) => { | ||
const results: IFunctionResult[] = []; | ||
const channelPointer = targetVal.channel?.$ref as string; // required | ||
|
||
targetVal.messages?.forEach((message, index) => { | ||
if (!message.$ref.startsWith(channelPointer)) { | ||
results.push({ | ||
message: 'Operation message does not belong to the specified channel.', | ||
path: [...ctx.path, 'messages', index], | ||
}); | ||
} | ||
}); | ||
|
||
return results; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ruleset'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
|
||
import { AsyncAPIFormats } from '../formats'; | ||
import { operationMessagesUnambiguity } from './functions/operationMessagesUnambiguity'; | ||
|
||
export const v3CoreRuleset = { | ||
description: 'Core AsyncAPI 3.x.x ruleset.', | ||
formats: AsyncAPIFormats.filterByMajorVersions(['3']).formats(), | ||
rules: { | ||
/** | ||
* Operation Object rules | ||
*/ | ||
'asyncapi3-operation-messages-from-referred-channel': { | ||
description: 'Operation "messages" must be a subset of the messages defined in the channel referenced in this operation.', | ||
message: '{{error}}', | ||
severity: 'error', | ||
recommended: true, | ||
resolved: false, // We use the JSON pointer to match the channel. | ||
given: [ | ||
'$.operations.*', | ||
'$.components.operations.*', | ||
], | ||
then: { | ||
function: operationMessagesUnambiguity, | ||
}, | ||
}, | ||
}, | ||
}; |
234 changes: 234 additions & 0 deletions
234
test/ruleset/rules/v3/asyncapi3-operation-messages-from-referred-channel.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
import { testRule, DiagnosticSeverity } from '../../tester'; | ||
|
||
testRule('asyncapi3-operation-messages-from-referred-channel', [ | ||
{ | ||
name: 'valid case - required channel', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - optional channel', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
}, | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid case - message from operation in root pointing to a message from an optional channel (same name) defined under components', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/components/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
}, | ||
components: { | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: | ||
'Operation message does not belong to the specified channel.', | ||
path: ['operations', 'UserSignedUp', 'messages', '0'], | ||
severity: DiagnosticSeverity.Error, | ||
} | ||
], | ||
}, | ||
{ | ||
name: 'invalid case - message from operation in components pointing to a message from a different channel defined under components', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
channels: { | ||
UserRemoved: { | ||
messages: { | ||
UserRemoved: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/components/channels/UserRemoved/messages/UserRemoved' | ||
} | ||
] | ||
} | ||
}, | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
message: | ||
'Operation message does not belong to the specified channel.', | ||
path: ['components', 'operations', 'UserSignedUp', 'messages', '0'], | ||
severity: DiagnosticSeverity.Error, | ||
} | ||
], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters