-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the rules for server variables in v3
- Loading branch information
Showing
3 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
test/ruleset/rules/v3/asyncapi3-server-variables.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { testRule, DiagnosticSeverity } from '../../tester'; | ||
|
||
testRule('asyncapi2-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, | ||
}, | ||
], | ||
}, | ||
]); |