/
asyncApi2ChannelParameters.ts
51 lines (44 loc) · 1.55 KB
/
asyncApi2ChannelParameters.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
import { createRulesetFunction } from '@stoplight/spectral-core';
import { parseUrlVariables } from './utils/parseUrlVariables';
import { getMissingProps } from './utils/getMissingProps';
import { getRedundantProps } from './utils/getRedundantProps';
import type { IFunctionResult } from '@stoplight/spectral-core';
export default createRulesetFunction<{ parameters: Record<string, unknown> }, null>(
{
input: {
type: 'object',
properties: {
parameters: {
type: 'object',
},
},
required: ['parameters'],
},
options: null,
},
function asyncApi2ChannelParameters(targetVal, _, ctx) {
const path = ctx.path[ctx.path.length - 1] as string;
const results: IFunctionResult[] = [];
const parameters = parseUrlVariables(path);
if (parameters.length === 0) return;
const missingParameters = getMissingProps(parameters, targetVal.parameters);
if (missingParameters.length) {
results.push({
message: `Not all channel's parameters are described with "parameters" object. Missed: ${missingParameters.join(
', ',
)}.`,
path: [...ctx.path, 'parameters'],
});
}
const redundantParameters = getRedundantProps(parameters, targetVal.parameters);
if (redundantParameters.length) {
redundantParameters.forEach(param => {
results.push({
message: `Channel's "parameters" object has redundant defined "${param}" parameter.`,
path: [...ctx.path, 'parameters', param],
});
});
}
return results;
},
);