-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rulesets): propagate review suggestions
- Loading branch information
1 parent
09a0dfa
commit 7f36799
Showing
8 changed files
with
176 additions
and
56 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
33 changes: 0 additions & 33 deletions
33
packages/rulesets/src/asyncapi/functions/asyncApi2UniquenessTags.ts
This file was deleted.
Oops, something went wrong.
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
48 changes: 48 additions & 0 deletions
48
packages/rulesets/src/oas/__tests__/openapi-tags-uniqueness.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,48 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import testRule from './__helpers__/tester'; | ||
|
||
testRule('openapi-tags-uniqueness', [ | ||
{ | ||
name: 'valid case', | ||
document: { | ||
swagger: '2.0', | ||
tags: [{ name: 'one' }, { name: 'two' }], | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'tags has duplicated names', | ||
document: { | ||
swagger: '2.0', | ||
tags: [{ name: 'one' }, { name: 'one' }], | ||
}, | ||
errors: [ | ||
{ | ||
message: '"tags" object contains duplicate tag name "one".', | ||
path: ['tags', '1', 'name'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
name: 'tags has duplicated more that two times this same name', | ||
document: { | ||
swagger: '2.0', | ||
tags: [{ name: 'one' }, { name: 'one' }, { name: 'two' }, { name: 'one' }], | ||
}, | ||
errors: [ | ||
{ | ||
message: '"tags" object contains duplicate tag name "one".', | ||
path: ['tags', '1', 'name'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
message: '"tags" object contains duplicate tag name "one".', | ||
path: ['tags', '3', 'name'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
]); |
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
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
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 @@ | ||
export { default as uniquenessTags } from './uniquenessTags'; |
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,61 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
|
||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
|
||
type Tags = Array<{ name: string }>; | ||
|
||
function getDuplicateTagNames(tags: Tags): string[] { | ||
const tagNames = tags.map(item => item.name); | ||
return tagNames.reduce((acc, item, idx, arr) => { | ||
if (arr.indexOf(item) !== idx && acc.indexOf(item) < 0) { | ||
acc.push(item); | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
} | ||
|
||
export default createRulesetFunction<Tags, null>( | ||
{ | ||
input: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
function uniquenessTags(targetVal, _, ctx) { | ||
const duplicatedTags = getDuplicateTagNames(targetVal); | ||
if (duplicatedTags.length === 0) return []; | ||
|
||
const results: IFunctionResult[] = []; | ||
|
||
duplicatedTags.map(duplicatedTag => { | ||
let checkedFirst = false; | ||
const duplicatedTags: number[] = []; | ||
targetVal.forEach((tag, index) => { | ||
if (tag.name === duplicatedTag) { | ||
if (!checkedFirst) { | ||
checkedFirst = true; | ||
return; | ||
} | ||
duplicatedTags.push(index); | ||
} | ||
}); | ||
|
||
results.push( | ||
...duplicatedTags.map(duplicatedIndex => ({ | ||
message: `"tags" object contains duplicate tag name "${duplicatedTag}".`, | ||
path: [...ctx.path, duplicatedIndex, 'name'], | ||
})), | ||
); | ||
}); | ||
|
||
return results; | ||
}, | ||
); |