Skip to content

Commit

Permalink
feat(rulesets): run linter
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed May 19, 2022
1 parent 3bf1b33 commit 4f83588
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ testRule('asyncapi-channel-parameters-defined', [
channels: {
'users/{userId}/signedUp': {
parameters: {
userId: {}
}
userId: {},
},
},
},
},
Expand All @@ -24,14 +24,14 @@ testRule('asyncapi-channel-parameters-defined', [
channels: {
'users/{userId}/{anotherParam}/signedUp': {
parameters: {
userId: {}
}
userId: {},
},
},
},
},
errors: [
{
message: 'Not all channel\'s parameters are described with \"parameters\" object. Missed: anotherParam.',
message: 'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam.',
path: ['channels', 'users/{userId}/{anotherParam}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -45,14 +45,15 @@ testRule('asyncapi-channel-parameters-defined', [
channels: {
'users/{userId}/{anotherParam1}/{anotherParam2}/signedUp': {
parameters: {
userId: {}
}
userId: {},
},
},
},
},
errors: [
{
message: 'Not all channel\'s parameters are described with \"parameters\" object. Missed: anotherParam1, anotherParam2.',
message:
'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam1, anotherParam2.',
path: ['channels', 'users/{userId}/{anotherParam1}/{anotherParam2}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -67,15 +68,15 @@ testRule('asyncapi-channel-parameters-defined', [
channels: {
'users/{userId}/{anotherParam}/signedUp': {
parameters: {
userId: {}
}
userId: {},
},
},
},
}
},
},
errors: [
{
message: 'Not all channel\'s parameters are described with \"parameters\" object. Missed: anotherParam.',
message: 'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam.',
path: ['components', 'channels', 'users/{userId}/{anotherParam}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -92,18 +93,18 @@ testRule('asyncapi-channel-parameters-defined', [
userId: {},
anotherParam1: {},
anotherParam2: {},
}
},
},
},
},
errors: [
{
message: 'Channel\'s \"parameters\" object has redundant defined \"anotherParam1\" parameter.',
message: 'Channel\'s "parameters" object has redundant defined "anotherParam1" parameter.',
path: ['channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Channel\'s \"parameters\" object has redundant defined \"anotherParam2\" parameter.',
message: 'Channel\'s "parameters" object has redundant defined "anotherParam2" parameter.',
path: ['channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -121,19 +122,19 @@ testRule('asyncapi-channel-parameters-defined', [
userId: {},
anotherParam1: {},
anotherParam2: {},
}
},
},
},
},
},
errors: [
{
message: 'Channel\'s \"parameters\" object has redundant defined \"anotherParam1\" parameter.',
message: 'Channel\'s "parameters" object has redundant defined "anotherParam1" parameter.',
path: ['components', 'channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Channel\'s \"parameters\" object has redundant defined \"anotherParam2\" parameter.',
message: 'Channel\'s "parameters" object has redundant defined "anotherParam2" parameter.',
path: ['components', 'channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ testRule('asyncapi-server-variables-defined', [
url: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {}
}
sub: {},
},
},
},
},
Expand All @@ -28,14 +28,14 @@ testRule('asyncapi-server-variables-defined', [
url: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {}
}
sub: {},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with \"variables\" object. Missed: anotherParam.',
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -51,14 +51,15 @@ testRule('asyncapi-server-variables-defined', [
url: '{sub}.{anotherParam1}.{anotherParam2}.stoplight.io',
protocol: 'https',
variables: {
sub: {}
}
sub: {},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with \"variables\" object. Missed: anotherParam1, anotherParam2.',
message:
'Not all server\'s variables are described with "variables" object. Missed: anotherParam1, anotherParam2.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -75,15 +76,15 @@ testRule('asyncapi-server-variables-defined', [
url: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {}
}
sub: {},
},
},
},
}
},
},
errors: [
{
message: 'Not all server\'s variables are described with \"variables\" object. Missed: anotherParam.',
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['components', 'servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -102,18 +103,18 @@ testRule('asyncapi-server-variables-defined', [
sub: {},
anotherParam1: {},
anotherParam2: {},
}
},
},
},
},
errors: [
{
message: 'Server\'s \"variables\" object has redundant defined \"anotherParam1\" url variable.',
message: 'Server\'s "variables" object has redundant defined "anotherParam1" url variable.',
path: ['servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s \"variables\" object has redundant defined \"anotherParam2\" url variable.',
message: 'Server\'s "variables" object has redundant defined "anotherParam2" url variable.',
path: ['servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
Expand All @@ -133,19 +134,19 @@ testRule('asyncapi-server-variables-defined', [
sub: {},
anotherParam1: {},
anotherParam2: {},
}
},
},
},
}
},
},
errors: [
{
message: 'Server\'s \"variables\" object has redundant defined \"anotherParam1\" url variable.',
message: 'Server\'s "variables" object has redundant defined "anotherParam1" url variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s \"variables\" object has redundant defined \"anotherParam2\" url variable.',
message: 'Server\'s "variables" object has redundant defined "anotherParam2" url variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export default createRulesetFunction<{ parameters: Record<string, unknown> }, nu
const path = ctx.path[ctx.path.length - 1] as string;
const results: IFunctionResult[] = [];

let parameters = parseUrlVariables(path);
const 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(', ')}.`,
message: `Not all channel's parameters are described with "parameters" object. Missed: ${missingParameters.join(
', ',
)}.`,
path: [...ctx.path, 'parameters'],
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { parseUrlVariables, getMissingProps, getRedundantProps } from './utils';

import type { IFunctionResult } from '@stoplight/spectral-core';

export default createRulesetFunction<{ url: string, variables: Record<string, unknown> }, null>(
export default createRulesetFunction<{ url: string; variables: Record<string, unknown> }, null>(
{
input: null,
options: null,
},
function asyncApi2ServerVariables(targetVal, _, ctx) {
const results: IFunctionResult[] = [];

let variables = parseUrlVariables(targetVal.url);
const variables = parseUrlVariables(targetVal.url);
if (!variables || 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(', ')}.`,
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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/rulesets/src/asyncapi/functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ export function parseUrlVariables(str: string): string[] | undefined {
if (typeof str !== 'string') return;
const variables = str.match(/{(.+?)}/g);
if (!variables || variables.length === 0) return;
return variables.map(v => v.slice(1,-1));
};
return variables.map(v => v.slice(1, -1));
}

export function getMissingProps(arr: string[] = [], obj: Record<string, unknown> = {}) {
if (!Object.keys(obj).length) return arr;
return arr.filter(val => {
return !obj.hasOwnProperty(val);
});
};
}

export function getRedundantProps(arr: string[] = [], obj: Record<string, unknown> = {}) {
if (!arr.length) return Object.keys(obj);
return Object.keys(obj).filter(val => {
return !arr.includes(val);
});
};
}
10 changes: 2 additions & 8 deletions packages/rulesets/src/asyncapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ export default {
severity: 'error',
type: 'validation',
recommended: true,
given: [
'$.channels.*',
'$.components.channels.*',
],
given: ['$.channels.*', '$.components.channels.*'],
then: {
function: asyncApi2ChannelParameters,
},
Expand Down Expand Up @@ -304,10 +301,7 @@ export default {
severity: 'error',
type: 'validation',
recommended: true,
given: [
'$.servers.*',
'$.components.servers.*',
],
given: ['$.servers.*', '$.components.servers.*'],
then: {
function: asyncApi2ServerVariables,
},
Expand Down

0 comments on commit 4f83588

Please sign in to comment.