Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added the rules for server variables in v3 #1030

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/ruleset/v3/functions/serverVariables3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

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

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

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

export const serverVariables3 = createRulesetFunction<{ host: string, pathname: string; variables: Record<string, unknown> }, null>(
{
input: {
type: 'object',
properties: {
host: {
type: 'string',
},
pathname: {
type: 'string',

Check failure on line 17 in src/ruleset/v3/functions/serverVariables3.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Expected indentation of 10 spaces but found 12
},
variables: {
type: 'object',
},
},
required: ['host', 'variables'],
},
options: null,
},
(targetVal, _, ctx) => {
const results: IFunctionResult[] = [];
const url = targetVal.host + targetVal.pathname;

const variables = parseUrlVariables(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}" host and pathname variable.`,
path: [...ctx.path, 'variables', variable],
});
});
}

return results;
},
);
17 changes: 16 additions & 1 deletion src/ruleset/v3/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { AsyncAPIFormats } from '../formats';
import { operationMessagesUnambiguity } from './functions/operationMessagesUnambiguity';
import { serverVariables3 } from './functions/serverVariables3';
import { pattern } from '@stoplight/spectral-functions';

export const v3CoreRuleset = {
Expand Down Expand Up @@ -56,6 +57,20 @@ export const v3CoreRuleset = {
match: '#\\/servers\\/', // If doesn't match, rule fails.
},
},
}
},

/**
* Server Object rules
*/
'asyncapi3-server-variables': {
description: 'Server variables must be defined and there must be no redundant variables.',
message: '{{error}}',
severity: 'error',
recommended: true,
given: ['$.servers.*', '$.components.servers.*'],
then: {
function: serverVariables3,
},
},
},
};
179 changes: 179 additions & 0 deletions test/ruleset/rules/v3/asyncapi3-server-variables.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { testRule, DiagnosticSeverity } from '../../tester';

testRule('asyncapi3-server-variables', [
{
name: 'valid case',
document: {
asyncapi: '3.0.0',
servers: {
production: {
host: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [],
},

{
name: 'server has not defined definition for one of the host variables',
document: {
asyncapi: '3.0.0',
servers: {
production: {
host: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has not defined definition for two of the host variables',
document: {
asyncapi: '3.0.0',
servers: {
production: {
host: '{sub}.{anotherParam1}.{anotherParam2}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [
{
message:
'Not all server\'s variables are described with "variables" object. Missed: anotherParam1, anotherParam2.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has not defined definition for pathname variables',
document: {
asyncapi: '3.0.0',
servers: {
production: {
host: '{sub}.stoplight.io',
pathname: '/{anotherParam}',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [
{
message:
'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has not defined definition for one of the host variables (in the components.servers)',
document: {
asyncapi: '3.0.0',
components: {
servers: {
production: {
host: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['components', 'servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has redundant host variables',
document: {
asyncapi: '3.0.0',
servers: {
production: {
host: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
errors: [
{
message: 'Server\'s "variables" object has redundant defined "anotherParam1" host and pathname variable.',
path: ['servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s "variables" object has redundant defined "anotherParam2" host and pathname variable.',
path: ['servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has redundant host variables (in the components.servers)',
document: {
asyncapi: '3.0.0',
components: {
servers: {
production: {
host: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
},
errors: [
{
message: 'Server\'s "variables" object has redundant defined "anotherParam1" host and pathname variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s "variables" object has redundant defined "anotherParam2" host and pathname variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
Loading