-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #704 from mkistler/tag-defined
Add rule to detect operation tags that are not defined in global tags object
- Loading branch information
Showing
9 changed files
with
230 additions
and
9 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
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
153 changes: 153 additions & 0 deletions
153
src/rulesets/oas/functions/__tests__/oasTagDefined.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,153 @@ | ||
import { RuleType, Spectral } from '../../../../index'; | ||
|
||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import { rules } from '../../index.json'; | ||
import oasTagDefined from '../oasTagDefined'; | ||
|
||
describe('oasTagDefined', () => { | ||
const s = new Spectral(); | ||
|
||
s.setFunctions({ oasTagDefined }); | ||
s.setRules({ | ||
'operation-tag-defined': Object.assign(rules['operation-tag-defined'], { | ||
recommended: true, | ||
type: RuleType[rules['operation-tag-defined'].type], | ||
}), | ||
}); | ||
|
||
test('validate a correct object', async () => { | ||
const results = await s.run({ | ||
tags: [ | ||
{ | ||
name: 'tag1', | ||
}, | ||
{ | ||
name: 'tag2', | ||
}, | ||
], | ||
paths: { | ||
'/path1': { | ||
get: { | ||
tags: ['tag1'], | ||
}, | ||
}, | ||
'/path2': { | ||
get: { | ||
tags: ['tag2'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(results.length).toEqual(0); | ||
}); | ||
|
||
test('return errors on undefined tag', async () => { | ||
const results = await s.run({ | ||
tags: [ | ||
{ | ||
name: 'tag1', | ||
}, | ||
], | ||
paths: { | ||
'/path1': { | ||
get: { | ||
tags: ['tag2'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
code: 'operation-tag-defined', | ||
message: 'Operation tags should be defined in global tags.', | ||
path: ['paths', '/path1', 'get', 'tags', '0'], | ||
range: { | ||
end: { | ||
character: 16, | ||
line: 10, | ||
}, | ||
start: { | ||
character: 10, | ||
line: 10, | ||
}, | ||
}, | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
]); | ||
}); | ||
|
||
test('return errors on undefined tags among defined tags', async () => { | ||
const results = await s.run({ | ||
tags: [ | ||
{ | ||
name: 'tag1', | ||
}, | ||
{ | ||
name: 'tag3', | ||
}, | ||
], | ||
paths: { | ||
'/path1': { | ||
get: { | ||
tags: ['tag1', 'tag2', 'tag3', 'tag4'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
code: 'operation-tag-defined', | ||
message: 'Operation tags should be defined in global tags.', | ||
path: ['paths', '/path1', 'get', 'tags', '1'], | ||
range: { | ||
end: { | ||
character: 16, | ||
line: 14, | ||
}, | ||
start: { | ||
character: 10, | ||
line: 14, | ||
}, | ||
}, | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'operation-tag-defined', | ||
message: 'Operation tags should be defined in global tags.', | ||
path: ['paths', '/path1', 'get', 'tags', '3'], | ||
range: { | ||
end: { | ||
character: 16, | ||
line: 16, | ||
}, | ||
start: { | ||
character: 10, | ||
line: 16, | ||
}, | ||
}, | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
]); | ||
}); | ||
|
||
test('resilient to no global tags or operation tags', async () => { | ||
const results = await s.run({ | ||
paths: { | ||
'/path1': { | ||
get: { | ||
operationId: 'id1', | ||
}, | ||
}, | ||
'/path2': { | ||
get: { | ||
operationId: 'id2', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(results.length).toEqual(0); | ||
}); | ||
}); |
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,36 @@ | ||
// This function will check an API doc to verify that any tag that appears on | ||
// an operation is also present in the global tags array. | ||
|
||
import { IFunction, IFunctionResult, Rule } from '../../../types'; | ||
|
||
export const oasTagDefined: IFunction<Rule> = (targetVal, _options, functionPaths) => { | ||
const results: IFunctionResult[] = []; | ||
|
||
const globalTags = (targetVal.tags || []).map(({ name }: { name: string }) => name); | ||
|
||
const { paths = {} } = targetVal; | ||
|
||
const validOperationKeys = ['get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'trace']; | ||
|
||
for (const path in paths) { | ||
if (Object.keys(paths[path]).length > 0) { | ||
for (const operation in paths[path]) { | ||
if (validOperationKeys.indexOf(operation) > -1) { | ||
const { tags = [] } = paths[path][operation]; | ||
tags.forEach((tag: string, index: number) => { | ||
if (globalTags.indexOf(tag) === -1) { | ||
results.push({ | ||
message: 'Operation tags should be defined in global tags.', | ||
path: ['paths', path, operation, 'tags', index], | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
export default oasTagDefined; |
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
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