/
asyncApi2ServerVariables.ts
53 lines (46 loc) · 1.57 KB
/
asyncApi2ServerVariables.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 { parseUrlVariables } from './utils/parseUrlVariables';
import { getMissingProps } from './utils/getMissingProps';
import { getRedundantProps } from './utils/getRedundantProps';
import type { IFunctionResult } from '@stoplight/spectral-core';
export default createRulesetFunction<{ url: string; variables: Record<string, unknown> }, null>(
{
input: {
type: 'object',
properties: {
url: {
type: 'string',
},
variables: {
type: 'object',
}
},
required: ['url', 'variables'],
},
options: null,
},
function asyncApi2ServerVariables(targetVal, _, ctx) {
const results: IFunctionResult[] = [];
const variables = parseUrlVariables(targetVal.url);
if (variables.length === 0) return results;
const missingVariables = getMissingProps(variables, targetVal.variables);
if (missingVariables.length) {
results.push({
message: `Not all server's variables are described with "variables" object. Missed: ${missingVariables.join(
', ',
)}.`,
path: [...ctx.path, 'variables'],
});
}
const redundantVariables = getRedundantProps(variables, targetVal.variables);
if (redundantVariables.length) {
redundantVariables.forEach(variable => {
results.push({
message: `Server's "variables" object has redundant defined "${variable}" url variable.`,
path: [...ctx.path, 'variables', variable],
});
});
}
return results;
},
);