-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create rule for v3 core ruleset
- Loading branch information
1 parent
8710f53
commit e552578
Showing
4 changed files
with
210 additions
and
0 deletions.
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,5 @@ | ||
--- | ||
"@asyncapi/parser": minor | ||
--- | ||
|
||
feat: create the rule `asyncapi3-channel-servers` for the v3 rule core ruleset |
54 changes: 54 additions & 0 deletions
54
packages/parser/src/ruleset/v3/functions/channelServers.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,54 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
|
||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
|
||
export const channelServers = createRulesetFunction< | ||
{ servers?: Record<string, unknown>; channels?: Record<string, { servers?: Array<string> }> }, | ||
null | ||
>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
servers: { | ||
type: 'object', | ||
}, | ||
channels: { | ||
type: 'object', | ||
additionalProperties: { | ||
type: 'object', | ||
properties: { | ||
servers: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
(targetVal) => { | ||
const results: IFunctionResult[] = []; | ||
if (!targetVal.channels) return results; | ||
const serverNames = Object.keys(targetVal.servers ?? {}); | ||
|
||
Object.entries(targetVal.channels ?? {}).forEach(([channelAddress, channel]) => { | ||
if (!channel.servers) return; | ||
|
||
channel.servers.forEach((serverName, index) => { | ||
if (!serverNames.includes(serverName)) { | ||
results.push({ | ||
message: 'Channel contains server that are not defined on the "servers" object.', | ||
path: ['channels', channelAddress, 'servers', index], | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
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
140 changes: 140 additions & 0 deletions
140
packages/parser/test/ruleset/rules/v3/asyncapi3-channel-servers.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,140 @@ | ||
import { testRule, DiagnosticSeverity } from '../../tester'; | ||
|
||
testRule('asyncapi3-channel-servers', [ | ||
{ | ||
name: 'valid case', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
channels: { | ||
channel: { | ||
servers: ['development'], | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case - without defined servers', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
channels: { | ||
channel: {}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case - without defined servers in the root', | ||
document: { | ||
asyncapi: '3.0.0', | ||
channels: { | ||
channel: {}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case - without defined channels in the root', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case - with empty array', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
channels: { | ||
channel: { | ||
servers: [], | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'invalid case', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
channels: { | ||
channel: { | ||
servers: ['another-server'], | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Channel contains server that are not defined on the "servers" object.', | ||
path: ['channels', 'channel', 'servers', '0'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'invalid case - one server is defined, another one not', | ||
document: { | ||
asyncapi: '3.0.0', | ||
servers: { | ||
development: {}, | ||
production: {}, | ||
}, | ||
channels: { | ||
channel: { | ||
servers: ['production', 'another-server'], | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Channel contains server that are not defined on the "servers" object.', | ||
path: ['channels', 'channel', 'servers', '1'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'invalid case - without defined servers', | ||
document: { | ||
asyncapi: '3.0.0', | ||
channels: { | ||
channel: { | ||
servers: ['production'], | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Channel contains server that are not defined on the "servers" object.', | ||
path: ['channels', 'channel', 'servers', '0'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
]); |