-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rulesets): check uniqueness of AsyncAPI operations (#2121)
- Loading branch information
1 parent
4447d81
commit 8b3cce4
Showing
5 changed files
with
302 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
172 changes: 172 additions & 0 deletions
172
packages/rulesets/src/asyncapi/__tests__/asyncapi-operation-operationId-uniqueness.test.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,172 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import testRule from './__helpers__/tester'; | ||
|
||
testRule('asyncapi-operation-operationId-uniqueness', [ | ||
{ | ||
name: 'validate a correct object', | ||
document: { | ||
asyncapi: '2.0.0', | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id3', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'return errors on different operations same id', | ||
document: { | ||
asyncapi: '2.0.0', | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: ['channels', 'someChannel2', 'publish', 'operationId'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'return errors on same path operations same id', | ||
document: { | ||
asyncapi: '2.0.0', | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id2', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: ['channels', 'someChannel2', 'publish', 'operationId'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'return errors on different operations same id (more than two operations)', | ||
document: { | ||
asyncapi: '2.0.0', | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel3: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
publish: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: ['channels', 'someChannel2', 'publish', 'operationId'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: ['channels', 'someChannel3', 'subscribe', 'operationId'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: ['channels', 'someChannel3', 'publish', 'operationId'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'do not check operationId in the components', | ||
document: { | ||
asyncapi: '2.3.0', | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id3', | ||
}, | ||
}, | ||
}, | ||
components: { | ||
channels: { | ||
someChannel1: { | ||
subscribe: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
someChannel2: { | ||
subscribe: { | ||
operationId: 'id2', | ||
}, | ||
publish: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
]); |
53 changes: 53 additions & 0 deletions
53
packages/rulesets/src/asyncapi/functions/asyncApi2OperationIdUniqueness.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,53 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
|
||
import { getAllOperations } from './utils/getAllOperations'; | ||
|
||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
|
||
export default createRulesetFunction< | ||
{ channels: Record<string, { subscribe: Record<string, unknown>; publish: Record<string, unknown> }> }, | ||
null | ||
>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
channels: { | ||
type: 'object', | ||
properties: { | ||
subscribe: { | ||
type: 'object', | ||
}, | ||
publish: { | ||
type: 'object', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
function asyncApi2OperationIdUniqueness(targetVal, _) { | ||
const results: IFunctionResult[] = []; | ||
const operations = getAllOperations(targetVal); | ||
|
||
const seenIds: unknown[] = []; | ||
for (const { path, operation } of operations) { | ||
if (!('operationId' in operation)) { | ||
continue; | ||
} | ||
|
||
const operationId = (operation as { operationId: string }).operationId; | ||
if (seenIds.includes(operationId)) { | ||
results.push({ | ||
message: '"operationId" must be unique across all the operations.', | ||
path: [...path, 'operationId'], | ||
}); | ||
} else { | ||
seenIds.push(operationId); | ||
} | ||
} | ||
|
||
return results; | ||
}, | ||
); |
36 changes: 36 additions & 0 deletions
36
packages/rulesets/src/asyncapi/functions/utils/getAllOperations.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,36 @@ | ||
import { isPlainObject } from '@stoplight/json'; | ||
|
||
import type { JsonPath } from '@stoplight/types'; | ||
|
||
type AsyncAPI = { | ||
channels?: Record<string, { subscribe?: Record<string, unknown>; publish?: Record<string, unknown> }>; | ||
}; | ||
type Operation = { path: JsonPath; kind: 'subscribe' | 'publish'; operation: Record<string, unknown> }; | ||
|
||
export function* getAllOperations(asyncapi: AsyncAPI): IterableIterator<Operation> { | ||
const channels = asyncapi?.channels; | ||
if (!isPlainObject(channels)) { | ||
return []; | ||
} | ||
|
||
for (const [channelAddress, channel] of Object.entries(channels)) { | ||
if (!isPlainObject(channel)) { | ||
continue; | ||
} | ||
|
||
if (isPlainObject(channel.subscribe)) { | ||
yield { | ||
path: ['channels', channelAddress, 'subscribe'], | ||
kind: 'subscribe', | ||
operation: channel.subscribe, | ||
}; | ||
} | ||
if (isPlainObject(channel.publish)) { | ||
yield { | ||
path: ['channels', channelAddress, 'publish'], | ||
kind: 'publish', | ||
operation: channel.publish, | ||
}; | ||
} | ||
} | ||
} |
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