Skip to content

Commit

Permalink
Add new Spectral rule to check for null addresses in channels associa…
Browse files Browse the repository at this point in the history
…ted with operations defining reply
  • Loading branch information
Gmin2 committed Nov 30, 2023
1 parent 5c833ce commit 801f5b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/ruleset/functions/checkNullAddressInOperations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IFunction, IFunctionResult } from '@stoplight/spectral-core';
import { IAsyncAPIOperation, IAsyncAPIChannel } from '@stoplight/types';
import { Operations } from 'models/v2/operations';

const checkNullAddressInOperations: IFunction = (document, opts) => {
const results: IFunctionResult[] = [];

const channels: Record<string, IAsyncAPIChannel> = document.channels ?? {};

for(const channelKey in channels){
const channel = channels[channelKey];

//check if channel has operation defined
if(channel.operations) {
for(const operationKey in channel.operations) {
const operation = channel.operations[operationKey];

//check if the operation defines a reply and the channel is null or undefined
if(operation.reply && (!channel.address || channel.address === 'null')) {
results.push({
message: `Channel "${channelKey}" associated with operation "${operationKey}" has a null or undefined address.`,
path: ['channels', channelKey, 'operations', operationKey, 'reply'],
severity: 'error'
})
}
}
}
}
return results;
}

export default checkNullAddressInOperations;
10 changes: 10 additions & 0 deletions src/ruleset/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export const coreRuleset = {
function: internal,
},
},
'asyncapi-null-address-in-operations': {
description: 'check for null adrresses in channels associated with operations defining reply',
message: 'channel address should not be null when operation define a reply',
severity: 'error',
recommended: true,
given : '$',
then: {
function: 'checkNullAddressInOperations',
},
}
},
};

Expand Down

0 comments on commit 801f5b3

Please sign in to comment.