Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Spectral rule to check for null addresses in channels associated with operations defining reply #916

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading