-
-
Notifications
You must be signed in to change notification settings - Fork 218
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(rulesets): add rule to check if messageId is defined #2281
Open
magicmatatjahu
wants to merge
3
commits into
stoplightio:develop
Choose a base branch
from
magicmatatjahu:asyncapi/operationId-optional
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
179 changes: 179 additions & 0 deletions
179
packages/rulesets/src/asyncapi/__tests__/asyncapi-message-messageId.test.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 { DiagnosticSeverity } from '@stoplight/types'; | ||
import testRule from './__helpers__/tester'; | ||
|
||
testRule('asyncapi-message-messageId', [ | ||
{ | ||
name: 'valid case', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: { | ||
messageId: 'firstId', | ||
}, | ||
}, | ||
subscribe: { | ||
message: { | ||
messageId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case (unsupported version)', | ||
document: { | ||
asyncapi: '2.3.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: {}, | ||
}, | ||
subscribe: { | ||
message: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'valid case (with traits)', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: { | ||
traits: [ | ||
{}, | ||
{ | ||
messageId: 'firstId', | ||
}, | ||
], | ||
}, | ||
}, | ||
subscribe: { | ||
message: { | ||
messageId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'invalid case', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: {}, | ||
}, | ||
subscribe: { | ||
message: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'publish', 'message'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'subscribe', 'message'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'invalid case (oneOf case)', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: {}, | ||
externalDocs: {}, | ||
}, | ||
subscribe: { | ||
message: { | ||
oneOf: [ | ||
{}, | ||
{ | ||
messageId: 'someId', | ||
}, | ||
{}, | ||
{ | ||
traits: [ | ||
{}, | ||
{ | ||
messageId: 'anotherId', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'publish', 'message'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'subscribe', 'message', 'oneOf', '0'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'subscribe', 'message', 'oneOf', '2'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'invalid case (with traits)', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
message: { | ||
traits: [{}, {}], | ||
}, | ||
}, | ||
subscribe: { | ||
message: { | ||
messageId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Message should have a "messageId" field defined.', | ||
path: ['channels', 'one', 'publish', 'message'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
]); |
99 changes: 77 additions & 22 deletions
99
packages/rulesets/src/asyncapi/__tests__/asyncapi-operation-operationId.test.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 |
---|---|---|
@@ -1,39 +1,94 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import produce from 'immer'; | ||
import testRule from './__helpers__/tester'; | ||
|
||
const document = { | ||
asyncapi: '2.0.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
operationId: 'onePubId', | ||
}, | ||
subscribe: { | ||
operationId: 'oneSubId', | ||
testRule('asyncapi-operation-operationId', [ | ||
{ | ||
name: 'valid case', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
operationId: 'firstId', | ||
}, | ||
subscribe: { | ||
operationId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
}; | ||
|
||
testRule('asyncapi-operation-operationId', [ | ||
{ | ||
name: 'valid case', | ||
document, | ||
name: 'valid case (with traits)', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
traits: [ | ||
{}, | ||
{ | ||
operationId: 'firstId', | ||
}, | ||
], | ||
}, | ||
subscribe: { | ||
operationId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
...['publish', 'subscribe'].map(property => ({ | ||
name: `channels.{channel}.${property}.operationId property is missing`, | ||
document: produce(document, draft => { | ||
delete draft.channels.one[property].operationId; | ||
}), | ||
{ | ||
name: 'invalid case', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: {}, | ||
subscribe: {}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Operation must have an "operationId" field defined.', | ||
path: ['channels', 'one', 'publish'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: 'Operation must have an "operationId" field defined.', | ||
path: ['channels', 'one', 'subscribe'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'invalid case (with traits)', | ||
document: { | ||
asyncapi: '2.4.0', | ||
channels: { | ||
one: { | ||
publish: { | ||
traits: [{}, {}], | ||
}, | ||
subscribe: { | ||
operationId: 'secondId', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Operation must have "operationId".', | ||
path: ['channels', 'one', property], | ||
message: 'Operation must have an "operationId" field defined.', | ||
path: ['channels', 'one', 'publish'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
})), | ||
}, | ||
]); |
34 changes: 34 additions & 0 deletions
34
packages/rulesets/src/asyncapi/functions/asyncApi2CheckId.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,34 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
import { truthy } from '@stoplight/spectral-functions'; | ||
import { mergeTraits } from './utils/mergeTraits'; | ||
|
||
import type { MaybeHaveTraits } from './utils/mergeTraits'; | ||
|
||
export default createRulesetFunction<MaybeHaveTraits, { idField: 'operationId' | 'messageId' }>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
traits: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
}, | ||
}, | ||
}, | ||
}, | ||
options: { | ||
type: 'object', | ||
properties: { | ||
idField: { | ||
type: 'string', | ||
enum: ['operationId', 'messageId'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
function asyncApi2CheckId(targetVal, options, ctx) { | ||
const mergedValue = mergeTraits(targetVal); | ||
return truthy(mergedValue[options.idField], null, ctx); | ||
}, | ||
); |
32 changes: 32 additions & 0 deletions
32
packages/rulesets/src/asyncapi/functions/utils/__tests__/mergeTraits.test.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,32 @@ | ||
import { mergeTraits } from '../mergeTraits'; | ||
|
||
describe('mergeTraits', () => { | ||
test('should merge one trait', () => { | ||
const result = mergeTraits({ payload: {}, traits: [{ payload: { someKey: 'someValue' } }] }); | ||
expect(result.payload).toEqual({ someKey: 'someValue' }); | ||
}); | ||
|
||
test('should merge two or more traits', () => { | ||
const result = mergeTraits({ | ||
payload: {}, | ||
traits: [ | ||
{ payload: { someKey1: 'someValue1' } }, | ||
{ payload: { someKey2: 'someValue2' } }, | ||
{ payload: { someKey3: 'someValue3' } }, | ||
], | ||
}); | ||
expect(result.payload).toEqual({ someKey1: 'someValue1', someKey2: 'someValue2', someKey3: 'someValue3' }); | ||
}); | ||
|
||
test('should override fields', () => { | ||
const result = mergeTraits({ | ||
payload: { someKey: 'someValue' }, | ||
traits: [ | ||
{ payload: { someKey: 'someValue1' } }, | ||
{ payload: { someKey: 'someValue2' } }, | ||
{ payload: { someKey: 'someValue3' } }, | ||
], | ||
}); | ||
expect(result.payload).toEqual({ someKey: 'someValue3' }); | ||
}); | ||
}); |
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.