forked from asyncapi/parser-js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rule to validate required operation channel field #5
Closed
smoya
wants to merge
2
commits into
feat/v3OperationMessagesRule
from
feat/v3RequiredOperationChannelRule
Closed
Changes from all commits
Commits
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
42 changes: 42 additions & 0 deletions
42
src/ruleset/v3/functions/requiredOperationChannelUnambiguity.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,42 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
import { SchemaDefinition } from '@stoplight/spectral-core/dist/ruleset/function'; | ||
|
||
const referenceSchema: SchemaDefinition = { | ||
type: 'object', | ||
properties: { | ||
$ref: { | ||
type: 'string', | ||
format: 'uri-reference' | ||
}, | ||
}, | ||
}; | ||
|
||
export const requiredOperationChannelUnambiguity = createRulesetFunction<{ channel?: {'$ref': string}; messages?: [{'$ref': string}] }, null>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
channel: referenceSchema, | ||
messages: { | ||
type: 'array', | ||
items: referenceSchema, | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
(targetVal, _, ctx) => { | ||
const results: IFunctionResult[] = []; | ||
const channelPointer = targetVal.channel?.$ref as string; // required | ||
|
||
if (!channelPointer.includes('#/channels/')) { | ||
results.push({ | ||
message: 'The channel field of a required operation should point to a required channel.', | ||
path: [...ctx.path, 'channel'], | ||
}); | ||
} | ||
|
||
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
261 changes: 261 additions & 0 deletions
261
test/ruleset/rules/v3/asyncapi3-required-operation-channel-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,261 @@ | ||
import { testRule, DiagnosticSeverity } from '../../tester'; | ||
|
||
testRule('asyncapi3-required-operation-channel-unambiguity', [ | ||
{ | ||
name: 'valid case - required operation (under root) channel field points to a required channel (under root)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - required operation (under root) channel field points to a required channel (under root) from an external doc', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: 'http://foo.bar/components/file.yml#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: 'http://foo.bar/components/file.yml#/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - required operation (under components) channel field points to a required channel (under root)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid case - optional operation (under components) channel field points to an optional channel (under components)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
components: { | ||
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 operation (in root) channel field points to an optional channel (under components)', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: '#/components/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: '#/components/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
}, | ||
components: { | ||
channels: { | ||
UserSignedUp: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string' | ||
}, | ||
email: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'The channel field of a required operation should point to a required channel.', | ||
path: ['operations', 'UserSignedUp', 'channel'], | ||
severity: DiagnosticSeverity.Error, | ||
} | ||
], | ||
}, | ||
{ | ||
name: 'invalid case - required operation (in root) channel field points to an optional channel (under components) from an external doc', | ||
document: { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0' | ||
}, | ||
operations: { | ||
UserSignedUp: { | ||
action: 'send', | ||
channel: { | ||
$ref: 'http://foo.bar/components/file.yml#/components/channels/UserSignedUp' | ||
}, | ||
messages: [ | ||
{ | ||
$ref: 'http://foo.bar/components/file.yml#/components/channels/UserSignedUp/messages/UserSignedUp' | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
message: 'The channel field of a required operation should point to a required channel.', | ||
path: ['operations', 'UserSignedUp', 'channel'], | ||
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.
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.
Answered in https://github.com/asyncapi/parser-js/pull/913/files#r1407464169