-
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: add new 'ibm-no-ref-in-example' rule (#669)
This commit introduces the new 'ibm-no-ref-in-example' rule which will check to make sure that "$ref" is not used in "example" fields. Note that $ref is valid in an "examples" field, but not in an "example" field. Signed-off-by: Phil Adams <[email protected]>
- Loading branch information
Showing
7 changed files
with
462 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,54 @@ | ||
/** | ||
* Copyright 2017 - 2024 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { isObject } = require('@ibm-cloud/openapi-ruleset-utilities'); | ||
const { LoggerFactory } = require('../utils'); | ||
|
||
let ruleId; | ||
let logger; | ||
|
||
module.exports = function (exampleObj, options, context) { | ||
if (!logger) { | ||
ruleId = context.rule.name; | ||
logger = LoggerFactory.getInstance().getLogger(ruleId); | ||
} | ||
return noRefInExample(exampleObj, context.path); | ||
}; | ||
|
||
/** | ||
* This function will perform a recursive check to make sure that the specified | ||
* "example" value does not contain a "$ref" field. | ||
* @param {*} example the example value to check | ||
* @param {*} path the location of 'example' within the OpenAPI document | ||
* @returns an array containing zero or more error objects | ||
*/ | ||
function noRefInExample(example, path) { | ||
logger.debug(`${ruleId}: checking example located at: ${path.join('.')}`); | ||
|
||
// If it's not an object, then bail out now since a $ref property is not possible. | ||
if (!isObject(example)) { | ||
return []; | ||
} | ||
|
||
const errors = []; | ||
|
||
// Check "example" for a $ref property. | ||
if ('$ref' in example) { | ||
logger.debug( | ||
`${ruleId}: found a $ref property at location: ${path.join('.')}.$ref` | ||
); | ||
errors.push({ | ||
message: '', | ||
path: [...path, '$ref'], | ||
}); | ||
} | ||
|
||
// Check each property of "example" recursively for an object containing a $ref property. | ||
for (const p in example) { | ||
errors.push(...noRefInExample(example[p], [...path, p])); | ||
} | ||
|
||
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,18 @@ | ||
/** | ||
* Copyright 2017 - 2024 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { oas3 } = require('@stoplight/spectral-formats'); | ||
const { noRefInExample } = require('../functions'); | ||
module.exports = { | ||
description: 'The use of $ref is not valid within an example field', | ||
message: '{{description}}', | ||
given: ['$..example'], | ||
severity: 'error', | ||
formats: [oas3], | ||
resolved: false, | ||
then: { | ||
function: noRefInExample, | ||
}, | ||
}; |
Oops, something went wrong.