-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: message-examples rule and check unused security schemes in 2.x.x (…
- Loading branch information
1 parent
eb36c8c
commit 6b43317
Showing
11 changed files
with
14,919 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
import { getAllOperations } from '../utils'; | ||
import { isObject } from '../../../utils'; | ||
|
||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
import type { v2 } from '../../../spec-types'; | ||
|
||
export const unusedSecuritySchemes = createRulesetFunction<v2.AsyncAPIObject, null>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
components: { | ||
type: 'object', | ||
}, | ||
}, | ||
required: ['components'], | ||
}, | ||
options: null, | ||
}, | ||
(targetVal) => { | ||
const securitySchemes = targetVal.components?.securitySchemes; | ||
if (!isObject(securitySchemes)) { | ||
return; | ||
} | ||
|
||
const usedSecuritySchemes: string[] = []; | ||
// collect all security requirements from servers | ||
if (isObject(targetVal.servers)) { | ||
Object.values(targetVal.servers).forEach(server => { | ||
if (Array.isArray(server.security)) { | ||
server.security.forEach(requirements => { | ||
usedSecuritySchemes.push(...Object.keys(requirements)); | ||
}); | ||
} | ||
}); | ||
} | ||
// collect all security requirements from operations | ||
const operations = getAllOperations(targetVal); | ||
for (const { operation } of operations) { | ||
if (Array.isArray(operation.security)) { | ||
operation.security.forEach(requirements => { | ||
usedSecuritySchemes.push(...Object.keys(requirements)); | ||
}); | ||
} | ||
} | ||
|
||
const usedSecuritySchemesSet = new Set(usedSecuritySchemes); | ||
const securitySchemesKinds = Object.keys(securitySchemes); | ||
|
||
const results: IFunctionResult[] = []; | ||
securitySchemesKinds.forEach(securitySchemeKind => { | ||
if (!usedSecuritySchemesSet.has(securitySchemeKind)) { | ||
results.push({ | ||
message: 'Potentially unused security scheme has been detected in AsyncAPI document.', | ||
path: ['components', 'securitySchemes', securitySchemeKind], | ||
}); | ||
} | ||
}); | ||
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
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
Oops, something went wrong.