forked from stoplightio/spectral
/
asyncApi2OperationIdUniqueness.ts
53 lines (47 loc) · 1.33 KB
/
asyncApi2OperationIdUniqueness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
},
);