Skip to content

Commit

Permalink
feat(rulesets): propagate review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed May 19, 2022
1 parent 4f83588 commit b33df5d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 28 deletions.
@@ -1,7 +1,7 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-parameters-defined', [
testRule('asyncapi-channel-parameters', [
{
name: 'valid case',
document: {
Expand Down
@@ -1,7 +1,7 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-server-variables-defined', [
testRule('asyncapi-server-variables', [
{
name: 'valid case',
document: {
Expand Down
@@ -1,5 +1,8 @@
import { createRulesetFunction } from '@stoplight/spectral-core';
import { parseUrlVariables, getMissingProps, getRedundantProps } from './utils';

import { parseUrlVariables } from './utils/parseUrlVariables';
import { getMissingProps } from './utils/getMissingProps';
import { getRedundantProps } from './utils/getRedundantProps';

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

Expand All @@ -13,7 +16,7 @@ export default createRulesetFunction<{ parameters: Record<string, unknown> }, nu
const results: IFunctionResult[] = [];

const parameters = parseUrlVariables(path);
if (!parameters || parameters.length === 0) return;
if (parameters.length === 0) return;

const missingParameters = getMissingProps(parameters, targetVal.parameters);
if (missingParameters.length) {
Expand Down
@@ -1,5 +1,8 @@
import { createRulesetFunction } from '@stoplight/spectral-core';
import { parseUrlVariables, getMissingProps, getRedundantProps } from './utils';

import { parseUrlVariables } from './utils/parseUrlVariables';
import { getMissingProps } from './utils/getMissingProps';
import { getRedundantProps } from './utils/getRedundantProps';

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

Expand All @@ -12,7 +15,7 @@ export default createRulesetFunction<{ url: string; variables: Record<string, un
const results: IFunctionResult[] = [];

const variables = parseUrlVariables(targetVal.url);
if (!variables || variables.length === 0) return results;
if (variables.length === 0) return results;

const missingVariables = getMissingProps(variables, targetVal.variables);
if (missingVariables.length) {
Expand Down
20 changes: 0 additions & 20 deletions packages/rulesets/src/asyncapi/functions/utils.ts

This file was deleted.

@@ -0,0 +1,18 @@
import { getMissingProps } from '../getMissingProps';

describe('getMissingProps', () => {
test('should return all props when object is empty', () => {
const result = getMissingProps(['one', 'two', 'three'], {});
expect(result).toEqual(['one', 'two', 'three']);
});

test('should return only missed props', () => {
const result = getMissingProps(['one', 'two', 'three'], { one: {}, three: {} });
expect(result).toEqual(['two']);
});

test('should return empty array when all props are defined', () => {
const result = getMissingProps(['one', 'two', 'three'], { one: {}, two: {}, three: {} });
expect(result).toEqual([]);
});
});
@@ -0,0 +1,18 @@
import { getRedundantProps } from '../getRedundantProps';

describe('getRedundantProps', () => {
test('should return all redundant props when array is empty', () => {
const result = getRedundantProps([], { one: {}, two: {}, three: {} });
expect(result).toEqual(['one', 'two', 'three']);
});

test('should return only redundant props', () => {
const result = getRedundantProps(['one', 'three'], { one: {}, two: {}, three: {} });
expect(result).toEqual(['two']);
});

test('should return empty array when all props are defined', () => {
const result = getRedundantProps(['one', 'two', 'three'], { one: {}, two: {}, three: {} });
expect(result).toEqual([]);
});
});
@@ -0,0 +1,13 @@
import { parseUrlVariables } from '../parseUrlVariables';

describe('parseUrlVariables', () => {
test('should return all variables from string', () => {
const result = parseUrlVariables('{stage}.some.{channel}');
expect(result).toEqual(['stage', 'channel']);
});

test('should return empty array if no variable is defined', () => {
const result = parseUrlVariables('stage.some.channel');
expect(result).toEqual([]);
});
});
@@ -0,0 +1,6 @@
export function getMissingProps(arr: string[] = [], obj: Record<string, unknown> = {}): string[] {
if (!Object.keys(obj).length) return arr;
return arr.filter(val => {
return !Object.prototype.hasOwnProperty.call(obj, val);
});
}
@@ -0,0 +1,6 @@
export function getRedundantProps(arr: string[] = [], obj: Record<string, unknown> = {}): string[] {
if (!arr.length) return Object.keys(obj);
return Object.keys(obj).filter(val => {
return !arr.includes(val);
});
}
@@ -0,0 +1,6 @@
export function parseUrlVariables(str: string): string[] {
if (typeof str !== 'string') return [];
const variables = str.match(/{(.+?)}/g);
if (!variables || variables.length === 0) return [];
return variables.map(v => v.slice(1, -1));
}
4 changes: 2 additions & 2 deletions packages/rulesets/src/asyncapi/index.ts
Expand Up @@ -57,7 +57,7 @@ export default {
},
},
},
'asyncapi-channel-parameters-defined': {
'asyncapi-channel-parameters': {
description: 'Channel parameters must be defined and there must be no redundant parameters.',
message: '{{error}}',
severity: 'error',
Expand Down Expand Up @@ -295,7 +295,7 @@ export default {
function: asyncApi2DocumentSchema,
},
},
'asyncapi-server-variables-defined': {
'asyncapi-server-variables': {
description: 'Server variables must be defined and there must be no redundant variables.',
message: '{{error}}',
severity: 'error',
Expand Down

0 comments on commit b33df5d

Please sign in to comment.