-
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-required-array-properties-in-response): add new validator ru…
…le (#684) This commit introduces the new 'ibm-required-array-properties-in-response' validator rule, which enforces API Handbook guidance related to array properties defined within response schemas. This rule will look for and warn about any array properties defined as optional within each response body's schema (and any sub-schemas reachable from there). Signed-off-by: Phil Adams <[email protected]>
- Loading branch information
Showing
16 changed files
with
463 additions
and
39 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
60 changes: 60 additions & 0 deletions
60
packages/ruleset/src/functions/required-array-properties-in-response.js
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,60 @@ | ||
/** | ||
* Copyright 2024 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { | ||
isArraySchema, | ||
isObject, | ||
validateSubschemas, | ||
} = require('@ibm-cloud/openapi-ruleset-utilities'); | ||
const { LoggerFactory } = require('../utils'); | ||
|
||
let ruleId; | ||
let logger; | ||
|
||
module.exports = function (schema, _opts, context) { | ||
if (!logger) { | ||
ruleId = context.rule.name; | ||
logger = LoggerFactory.getInstance().getLogger(ruleId); | ||
} | ||
|
||
return validateSubschemas( | ||
schema, | ||
context.path, | ||
checkForOptionalArrays, | ||
true, | ||
false | ||
); | ||
}; | ||
|
||
/** | ||
* Checks "schema" for any optional array properties. | ||
* @param {*} schema the schema to check | ||
* @param {*} path the json path location of "schema" | ||
* @returns | ||
*/ | ||
function checkForOptionalArrays(schema, path) { | ||
// If "schema" defines properties, then add an error for each optional array property. | ||
if (isObject(schema) && isObject(schema.properties)) { | ||
logger.debug( | ||
`${ruleId}: examining object schema at location: ${path.join('.')}` | ||
); | ||
|
||
const errors = []; | ||
|
||
const requiredProps = schema.required || []; | ||
for (const [name, prop] of Object.entries(schema.properties)) { | ||
if (isArraySchema(prop) && !requiredProps.includes(name)) { | ||
errors.push({ | ||
message: 'In a response body, an array field MUST NOT be optional', | ||
path: [...path, 'properties', name], | ||
}); | ||
} | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
return []; | ||
} |
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
21 changes: 21 additions & 0 deletions
21
packages/ruleset/src/rules/required-array-properties-in-response.js
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,21 @@ | ||
/** | ||
* Copyright 2017 - 2024 IBM Corporation. | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
const { | ||
responseSchemas, | ||
} = require('@ibm-cloud/openapi-ruleset-utilities/src/collections'); | ||
|
||
const { requiredArrayPropertiesInResponse } = require('../functions'); | ||
|
||
module.exports = { | ||
description: 'Array properties defined in a response should be required.', | ||
message: '{{error}}', | ||
given: responseSchemas, | ||
severity: 'error', | ||
resolved: true, | ||
then: { | ||
function: requiredArrayPropertiesInResponse, | ||
}, | ||
}; |
Oops, something went wrong.