-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial Spectral integration (#195)
* feat: initial Spectral integration * PR feedback, test enhancements, & including spectral in api invocation * use default yml ruleset file & support yml custom rulesets * More PR feedback. Improve unit test performance
- Loading branch information
Showing
24 changed files
with
4,130 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ dist | |
bin/ | ||
.nyc_output/ | ||
coverage/ | ||
.spectral.json | ||
.spectral.yaml | ||
.spectral.yml |
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
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,24 @@ | ||
extends: [[spectral:oas, off]] | ||
formats: [oas2, oas3] | ||
functionsDir": ../functions | ||
rules: | ||
no-eval-in-markdown: true | ||
no-script-tags-in-markdown: true | ||
openapi-tags: true | ||
operation-description: true | ||
operation-tags: true | ||
operation-tag-defined: true | ||
path-keys-no-trailing-slash: true | ||
typed-enum: true | ||
oas2-api-host: true | ||
oas2-api-schemes: true | ||
oas2-host-trailing-slash: true | ||
oas2-valid-example: true | ||
oas2-valid-definition-example: true | ||
oas2-anyOf: true | ||
oas2-oneOf: true | ||
oas3-api-servers: true | ||
oas3-examples-value-or-externalValue: true | ||
oas3-server-trailing-slash: true | ||
oas3-valid-example: true | ||
oas3-valid-schema-example: true |
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,74 @@ | ||
const MessageCarrier = require('../../plugins/utils/messageCarrier'); | ||
const config = require('../../cli-validator/utils/processConfiguration'); | ||
const { isOpenApiv2, isOpenApiv3 } = require('@stoplight/spectral'); | ||
// default spectral ruleset file | ||
const defaultSpectralRuleset = | ||
__dirname + '/../rulesets/.defaultsForSpectral.yaml'; | ||
|
||
const parseResults = function(results, debug) { | ||
const messages = new MessageCarrier(); | ||
|
||
if (results) { | ||
for (const validationResult of results) { | ||
if (validationResult) { | ||
const code = validationResult['code']; | ||
const severity = validationResult['severity']; | ||
const message = validationResult['message']; | ||
const path = validationResult['path']; | ||
|
||
if (typeof severity === 'number' && code && message && path) { | ||
if (code === 'parser') { | ||
// Spectral doesn't allow disabling parser rules, so don't include them | ||
// in the output (for now) | ||
continue; | ||
} | ||
// Our validator only supports warning/error level, so only include | ||
// those validation results (for now) | ||
if (severity === 1) { | ||
// warning | ||
messages.addMessage(path, message, 'warning'); | ||
} else if (severity === 0) { | ||
// error | ||
messages.addMessage(path, message, 'error'); | ||
} | ||
} else { | ||
if (debug) { | ||
console.log( | ||
'There was an error while parsing the spectral results: ', | ||
JSON.stringify(validationResult) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return messages; | ||
}; | ||
|
||
// setup: registers the oas2/oas3 formats, and attempts to load the ruleset file | ||
const setup = async function(spectral) { | ||
if (!spectral) { | ||
const message = | ||
'Error (spectral-validator): An instance of spectral has not been initialized.'; | ||
return Promise.reject(message); | ||
} | ||
|
||
spectral.registerFormat('oas2', isOpenApiv2); | ||
spectral.registerFormat('oas3', isOpenApiv3); | ||
|
||
// load the spectral ruleset, either a user's or the default ruleset | ||
const spectralRuleset = await config.getSpectralRuleset( | ||
defaultSpectralRuleset | ||
); | ||
|
||
try { | ||
return await spectral.loadRuleset(spectralRuleset); | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
parseResults, | ||
setup | ||
}; |
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
Oops, something went wrong.