/
asyncApi2ChannelParameters.ts
38 lines (32 loc) · 1.3 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
import { createRulesetFunction } from '@stoplight/spectral-core';
import { parseUrlVariables, getMissingProps, getRedundantProps } from './utils';
import type { IFunctionResult } from '@stoplight/spectral-core';
export default createRulesetFunction<{ parameters: Record<string, unknown> }, null>(
{
input: null,
options: null,
},
function asyncApi2ChannelParameters(targetVal, _, ctx) {
const path = ctx.path[ctx.path.length - 1] as string;
const results: IFunctionResult[] = [];
let parameters = parseUrlVariables(path);
if (!parameters || 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;
},
);