-
-
Notifications
You must be signed in to change notification settings - Fork 98
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: add Spectral rules to validate required operation channe and channel servers field #913
Merged
asyncapi-bot
merged 5 commits into
asyncapi:next-major-spec
from
smoya:feat/v3OperationChannelRule
Nov 28, 2023
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40d8e7a
feat: add Spectral rule to validate required operation channel field
smoya e17e7cd
Use Spectral `pattern` function
smoya 983b469
remove import
smoya 99e38cd
feat: add Spectral rule to validate required channel servers field
smoya 238dcda
fix typo
smoya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
|
||||||
import { AsyncAPIFormats } from '../formats'; | ||||||
import { operationMessagesUnambiguity } from './functions/operationMessagesUnambiguity'; | ||||||
import { pattern } from '@stoplight/spectral-functions'; | ||||||
|
||||||
export const v3CoreRuleset = { | ||||||
description: 'Core AsyncAPI 3.x.x ruleset.', | ||||||
|
@@ -24,5 +25,37 @@ export const v3CoreRuleset = { | |||||
function: operationMessagesUnambiguity, | ||||||
}, | ||||||
}, | ||||||
'asyncapi3-required-operation-channel-unambiguity': { | ||||||
description: 'The "channel" field of an operation under the root "operations" object must always reference a channel under the root "channels" object.', | ||||||
severity: 'error', | ||||||
recommended: true, | ||||||
resolved: false, // We use the JSON pointer to match the channel. | ||||||
given: '$.operations.*', | ||||||
then: { | ||||||
field: 'channel.$ref', | ||||||
function: pattern, | ||||||
functionOptions: { | ||||||
match: '#\\/channels\\/', // If doesn't match, rule fails. | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
|
||||||
/** | ||||||
* Channel Object rules | ||||||
*/ | ||||||
'asyncapi3-required-channel-servers-unambiguity': { | ||||||
description: 'The "servers" field of a channel under the root "channels" object must always reference to a subset of the servers under the root "servers" object.', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed (didnt apply the suggestion because changes in the expected error msg in tests were needed) |
||||||
severity: 'error', | ||||||
recommended: true, | ||||||
resolved: false, // We use the JSON pointer to match the channel. | ||||||
given: '$.channels.*', | ||||||
then: { | ||||||
field: '$.servers.*.$ref', | ||||||
function: pattern, | ||||||
functionOptions: { | ||||||
match: '#\\/servers\\/', // If doesn't match, rule fails. | ||||||
}, | ||||||
}, | ||||||
} | ||||||
}, | ||||||
}; |
210 changes: 210 additions & 0 deletions
210
test/ruleset/rules/v3/asyncapi3-required-channel-servers-unambiguity.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,210 @@ | ||
import { testRule, DiagnosticSeverity } from '../../tester'; | ||
|
||
testRule('asyncapi3-required-channel-servers-unambiguity', [ | ||
{ | ||
name: 'valid case - required channel (under root) server field points to a subset of required servers (under root)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
servers: { | ||
prod: { | ||
host: 'my-api.com', | ||
protocol: 'ws', | ||
}, | ||
dev: { | ||
host: 'localhost', | ||
protocol: 'ws', | ||
}, | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
servers: [ | ||
{ $ref: '#/servers/prod' }, | ||
{ $ref: '#/servers/dev' }, | ||
] | ||
} | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - required channel (under root) server field points to a subset of required servers (under root) from an external doc', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
servers: [ | ||
{ $ref: 'http://foo.bar/components/file.yml#/servers/prod' }, | ||
{ $ref: 'http://foo.bar/components/file.yml#/servers/dev' }, | ||
] | ||
} | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - optional channel (under components) server field points to a subset of required servers (under root)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
servers: { | ||
prod: { | ||
host: 'my-api.com', | ||
protocol: 'ws', | ||
}, | ||
dev: { | ||
host: 'localhost', | ||
protocol: 'ws', | ||
}, | ||
}, | ||
components: { | ||
channels: { | ||
UserSignedUp: { | ||
servers: [ | ||
{ $ref: '#/servers/prod' }, | ||
{ $ref: '#/servers/dev' }, | ||
] | ||
} | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - optional channel (under components) server field points to a subset of optional servers (under components)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
components: { | ||
servers: { | ||
prod: { | ||
host: 'my-api.com', | ||
protocol: 'ws', | ||
}, | ||
dev: { | ||
host: 'localhost', | ||
protocol: 'ws', | ||
}, | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/components/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/components/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid case - required channel (in root) servers field points to a subset of optional servers (under components)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
servers: [ | ||
{ $ref: '#/components/servers/prod' }, | ||
{ $ref: '#/components/servers/dev' }, | ||
] | ||
} | ||
}, | ||
components: { | ||
servers: { | ||
prod: { | ||
host: 'my-api.com', | ||
protocol: 'ws', | ||
}, | ||
dev: { | ||
host: 'localhost', | ||
protocol: 'ws', | ||
}, | ||
} | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
message: 'The "servers" field of a channel under the root "channels" object must always reference to a subset of the servers under the root "servers" object.', | ||
path: ['channels', 'UserSignedUp', 'servers', '0', '$ref'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: 'The "servers" field of a channel under the root "channels" object must always reference to a subset of the servers under the root "servers" object.', | ||
path: ['channels', 'UserSignedUp', 'servers', '1', '$ref'], | ||
severity: DiagnosticSeverity.Error, | ||
} | ||
], | ||
}, | ||
{ | ||
name: 'invalid case - required channel (in root) servers field points to a subset of optional servers (under components) from an external doc', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
servers: [ | ||
{ $ref: 'http://foo.bar/components/file.yml#/components/servers/prod' }, | ||
{ $ref: 'http://foo.bar/components/file.yml#/components/servers/dev' }, | ||
] | ||
} | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
message: 'The "servers" field of a channel under the root "channels" object must always reference to a subset of the servers under the root "servers" object.', | ||
path: ['channels', 'UserSignedUp', 'servers', '0', '$ref'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: 'The "servers" field of a channel under the root "channels" object must always reference to a subset of the servers under the root "servers" object.', | ||
path: ['channels', 'UserSignedUp', 'servers', '1', '$ref'], | ||
severity: DiagnosticSeverity.Error, | ||
} | ||
], | ||
}, | ||
]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fmvilas @jonaslagoni I simplified this by using this
pattern
function Spectral gives us. It allows to match by a regex applied to a field, in this casechannel.$ref
.In consequence, I removed the function I created in favor of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better 👍