Skip to content

Commit

Permalink
added condition to check parameter is null or param.length === 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ibishal committed Jun 11, 2024
1 parent f7feb39 commit 0a6d66a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ruleset/v2/functions/channelParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export const channelParameters = createRulesetFunction<{ parameters: Record<stri
const results: IFunctionResult[] = [];

const parameters = parseUrlVariables(path);
if (parameters.length === 0) return;

const hasParameters = Object.keys(targetVal.parameters).length > 0;
if (hasParameters && (path === null || parameters.length === 0)) {
results.push({
message: `Channel has parameters defined, but the address is null or does not contain any parameters.`,
path: ctx.path.slice(0, -1),
});
return results;
}

const missingParameters = getMissingProps(parameters, targetVal.parameters);
if (missingParameters.length) {
Expand Down
19 changes: 19 additions & 0 deletions test/ruleset/rules/v2/asyncapi2-channel-parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ testRule('asyncapi2-channel-parameters', [
errors: [],
},

{
name: 'address has no parameters but parameters are defined',
document: {
asyncapi: '2.0.0',
channels: {
'users/signedUp': {
parameters: {
test: {},
},
},
},
},
errors: [{
message: 'Channel has parameters defined, but the address is null or does not contain any parameters',
path: ['channels', 'users/signedUp'],
severity: DiagnosticSeverity.Error,
},],
},

{
name: 'channel has not defined definition for one of the parameters',
document: {
Expand Down

0 comments on commit 0a6d66a

Please sign in to comment.