-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ibm-no-unsupported-keywords): add new validation rule
This commit introduces the new 'ibm-no-unsupported-keywords' validation rule which checks to make sure that the (currently) unsupported keywords 'jsonSchemaDialect' and 'webhooks' are not present in an OpenAPI 3.1 document. Signed-off-by: Phil Adams <[email protected]>
- Loading branch information
Showing
7 changed files
with
232 additions
and
0 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
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,53 @@ | ||
/** | ||
* Copyright 2017 - 2023 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { LoggerFactory } = require('../utils'); | ||
|
||
let ruleId; | ||
let logger; | ||
|
||
const ErrorMsg = | ||
'An unsupported OpenAPI 3.1 keyword was found in the OpenAPI document:'; | ||
|
||
module.exports = function (apidef, _opts, context) { | ||
if (!logger) { | ||
ruleId = context.rule.name; | ||
logger = LoggerFactory.getInstance().getLogger(ruleId); | ||
} | ||
return noUnsupportedKeywords(apidef); | ||
}; | ||
|
||
/** | ||
* If 'unevaluatedProperties' is specified within "schema" then it must be set to false. | ||
* | ||
* @param {*} apidef the API definition object | ||
* @returns an array of zero or more errors | ||
*/ | ||
function noUnsupportedKeywords(apidef) { | ||
logger.debug(`${ruleId}: checking for unsupported OpenAPI 3.1 keywords`); | ||
|
||
const errors = []; | ||
|
||
if ('jsonSchemaDialect' in apidef) { | ||
logger.debug(`${ruleId}: found 'jsonSchemaDialect'`); | ||
errors.push({ | ||
message: `${ErrorMsg} jsonSchemaDialect`, | ||
path: ['jsonSchemaDialect'], | ||
}); | ||
} | ||
|
||
if ('webhooks' in apidef) { | ||
logger.debug(`${ruleId}: found 'webhooks`); | ||
errors.push({ | ||
message: `${ErrorMsg} webhooks`, | ||
path: ['webhooks'], | ||
}); | ||
} | ||
|
||
if (!errors.length) { | ||
logger.debug(`${ruleId}: PASSED!`); | ||
} | ||
return errors; | ||
} |
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,20 @@ | ||
/** | ||
* Copyright 2017 - 2023 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { oas3_1 } = require('@stoplight/spectral-formats'); | ||
const { noUnsupportedKeywords } = require('../functions'); | ||
|
||
module.exports = { | ||
description: | ||
'Verifies that unsupported OpenAPI 3.1 keywords are not used in the API document.', | ||
message: '{{error}}', | ||
given: ['$'], | ||
severity: 'error', | ||
formats: [oas3_1], | ||
resolved: false, | ||
then: { | ||
function: noUnsupportedKeywords, | ||
}, | ||
}; |
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,79 @@ | ||
/** | ||
* Copyright 2017 - 2023 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { noUnsupportedKeywords } = require('../src/rules'); | ||
const { makeCopy, rootDocument, testRule, severityCodes } = require('./utils'); | ||
|
||
const rule = noUnsupportedKeywords; | ||
const ruleId = 'ibm-no-unsupported-keywords'; | ||
const expectedSeverity = severityCodes.error; | ||
const expectedMsgPrefix = | ||
'An unsupported OpenAPI 3.1 keyword was found in the OpenAPI document:'; | ||
|
||
describe(`Spectral rule: ${ruleId}`, () => { | ||
beforeAll(() => { | ||
rootDocument.openapi = '3.1.0'; | ||
}); | ||
|
||
describe('Should not yield errors', () => { | ||
it('Clean spec - no unsupported keywords present', async () => { | ||
const results = await testRule(ruleId, rule, rootDocument); | ||
expect(results).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe('Should yield errors', () => { | ||
it('jsonSchemaDialect present', async () => { | ||
const testDocument = makeCopy(rootDocument); | ||
|
||
testDocument.jsonSchemaDialect = | ||
'https://spec.openapis.org/oas/3.1/dialect/base'; | ||
|
||
const results = await testRule(ruleId, rule, testDocument); | ||
expect(results).toHaveLength(1); | ||
|
||
const expectedPaths = ['jsonSchemaDialect']; | ||
for (let i = 0; i < results.length; i++) { | ||
expect(results[i].code).toBe(ruleId); | ||
expect(results[i].message).toBe( | ||
`${expectedMsgPrefix} jsonSchemaDialect` | ||
); | ||
expect(results[i].severity).toBe(expectedSeverity); | ||
expect(results[i].path.join('.')).toBe(expectedPaths[i]); | ||
} | ||
}); | ||
it('webhooks present', async () => { | ||
const testDocument = makeCopy(rootDocument); | ||
|
||
testDocument.webhooks = { | ||
newDrinkAvailable: { | ||
post: { | ||
requestBody: { | ||
description: 'A new brand of beer is available for consumption.', | ||
content: { | ||
'application/beer': { | ||
schema: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = await testRule(ruleId, rule, testDocument); | ||
expect(results).toHaveLength(1); | ||
|
||
const expectedPaths = ['webhooks']; | ||
for (let i = 0; i < results.length; i++) { | ||
expect(results[i].code).toBe(ruleId); | ||
expect(results[i].message).toBe(`${expectedMsgPrefix} webhooks`); | ||
expect(results[i].severity).toBe(expectedSeverity); | ||
expect(results[i].path.join('.')).toBe(expectedPaths[i]); | ||
} | ||
}); | ||
}); | ||
}); |