|
| 1 | +import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js'; |
| 2 | +import { resolveObject } from './utils/componentUtils.js'; |
| 3 | +import { hasException } from './utils/exceptions.js'; |
| 4 | +import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; |
| 5 | +import { findPropertiesByAttribute } from './utils/schemaUtils.js'; |
| 6 | + |
| 7 | +const RULE_NAME = 'xgen-IPA-107-update-method-request-has-no-readonly-fields'; |
| 8 | +const ERROR_MESSAGE = 'The Update method request object must not include input fields (readOnly properties).'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Update method (PUT, PATCH) request objects must not include input fields (readOnly properties). |
| 12 | + * |
| 13 | + * @param {object} input - An update operation request content version |
| 14 | + * @param {object} _ - Unused |
| 15 | + * @param {{ path: string[], documentInventory: object}} context - The context object containing the path and document |
| 16 | + */ |
| 17 | +export default (input, _, { path, documentInventory }) => { |
| 18 | + const resourcePath = path[1]; |
| 19 | + const oas = documentInventory.resolved; |
| 20 | + |
| 21 | + if (!isSingleResourceIdentifier(resourcePath) || !input.endsWith('json')) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const requestContentPerMediaType = resolveObject(oas, path); |
| 26 | + if (!requestContentPerMediaType || !requestContentPerMediaType.schema) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (hasException(requestContentPerMediaType, RULE_NAME)) { |
| 31 | + collectException(requestContentPerMediaType, RULE_NAME, path); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const errors = checkViolationsAndReturnErrors(requestContentPerMediaType, path); |
| 36 | + |
| 37 | + if (errors.length !== 0) { |
| 38 | + return collectAndReturnViolation(path, RULE_NAME, errors); |
| 39 | + } |
| 40 | + |
| 41 | + collectAdoption(path, RULE_NAME); |
| 42 | +}; |
| 43 | + |
| 44 | +function checkViolationsAndReturnErrors(contentPerMediaType, path) { |
| 45 | + return findPropertiesByAttribute(contentPerMediaType.schema, 'readOnly', path, [], ERROR_MESSAGE); |
| 46 | +} |
0 commit comments