Skip to content

Commit fe9db99

Browse files
feat(ipa): error on unneeded exceptions IPA 108-114 (#880)
1 parent 72f7ed3 commit fe9db99

25 files changed

+97
-394
lines changed

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ overrides:
133133
- '**#/paths/~1api~1atlas~1v2~1groups~1%7BgroupId%7D~1pushBasedLogExport'
134134
rules:
135135
xgen-IPA-106-create-method-request-has-no-readonly-fields: 'off'
136+
- files: # To be removed in CLOUDP-337392
137+
- '**#/components/schemas/AWSHardwareSpec20240805/properties/ebsVolumeType'
138+
rules:
139+
xgen-IPA-112-field-names-are-camel-case: 'off'

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethod204Response.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import {
2-
collectAdoption,
3-
collectAndReturnViolation,
4-
collectException,
5-
handleInternalError,
6-
} from './utils/collectionUtils.js';
1+
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
72
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
8-
import { hasException } from './utils/exceptions.js';
93

104
const RULE_NAME = 'xgen-IPA-108-delete-method-return-204-response';
115
const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code.';
@@ -25,16 +19,8 @@ export default (input, _, { path }) => {
2519
return;
2620
}
2721

28-
if (hasException(input, RULE_NAME)) {
29-
collectException(input, RULE_NAME, path);
30-
return;
31-
}
32-
3322
const errors = checkViolationsAndReturnErrors(input, path);
34-
if (errors.length !== 0) {
35-
return collectAndReturnViolation(path, RULE_NAME, errors);
36-
}
37-
collectAdoption(path, RULE_NAME);
23+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3824
};
3925

4026
function checkViolationsAndReturnErrors(input, path) {

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethodNoRequestBody.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
1+
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
22
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
3-
import { hasException } from './utils/exceptions.js';
43

54
const RULE_NAME = 'xgen-IPA-108-delete-request-no-body';
65
const ERROR_MESSAGE = 'DELETE method should not have a request body.';
@@ -20,14 +19,14 @@ export default (input, _, { path }) => {
2019
return;
2120
}
2221

23-
if (hasException(input, RULE_NAME)) {
24-
collectException(input, RULE_NAME, path);
25-
return;
26-
}
27-
28-
const requestBody = input.requestBody;
29-
if (requestBody) {
30-
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
22+
let errors = [];
23+
if (input.requestBody) {
24+
errors = [
25+
{
26+
path,
27+
message: ERROR_MESSAGE,
28+
},
29+
];
3130
}
32-
return collectAdoption(path, RULE_NAME);
31+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3332
};

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethodResponseShouldNotHaveSchema.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import { hasException } from './utils/exceptions.js';
2-
import {
3-
collectAdoption,
4-
collectAndReturnViolation,
5-
collectException,
6-
handleInternalError,
7-
} from './utils/collectionUtils.js';
1+
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
82
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
93

104
const RULE_NAME = 'xgen-IPA-108-delete-response-should-be-empty';
@@ -22,26 +16,15 @@ export default (input, _, { path }) => {
2216
return;
2317
}
2418

25-
// 1. Handle exception on OpenAPI schema
26-
if (hasException(input, RULE_NAME)) {
27-
collectException(input, RULE_NAME, path);
28-
return;
29-
}
30-
31-
// 2. Validation
3219
const errors = checkViolationsAndReturnErrors(input, path);
33-
if (errors.length > 0) {
34-
return collectAndReturnViolation(path, RULE_NAME, errors);
35-
}
36-
37-
collectAdoption(path, RULE_NAME);
20+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3821
};
3922

4023
/**
4124
* Check if the operation has validation issues
42-
* @param {object} input - The object to vefify
43-
* @param {object} jsonPathArray - The jsonPathArray covering location in the OpenAPI schema
44-
* @return {Array<string>} - errors array ()
25+
* @param {object} input - The object to verify
26+
* @param {Array<string>} jsonPathArray - The jsonPathArray covering location in the OpenAPI schema
27+
* @return {Array<{path: Array<string>, message: string}>} - errors array ()
4528
*/
4629
function checkViolationsAndReturnErrors(input, jsonPathArray) {
4730
const errors = [];

tools/spectral/ipa/rulesets/functions/IPA108ValidOperationID.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import { hasException } from './utils/exceptions.js';
2-
import {
3-
collectAdoption,
4-
collectException,
5-
collectAndReturnViolation,
6-
handleInternalError,
7-
} from './utils/collectionUtils.js';
1+
import { handleInternalError, evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
82
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
93
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
104
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
@@ -18,23 +12,13 @@ export default (input, { methodName }, { path }) => {
1812
return;
1913
}
2014

21-
if (hasException(input, RULE_NAME)) {
22-
collectException(input, RULE_NAME, path);
23-
return;
24-
}
25-
2615
if (hasMethodVerbOverride(input, methodName)) {
2716
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
2817
}
2918

3019
try {
3120
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
32-
33-
if (errors.length > 0) {
34-
return collectAndReturnViolation(path, RULE_NAME, errors);
35-
}
36-
37-
collectAdoption(path, RULE_NAME);
21+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3822
} catch (e) {
3923
return handleInternalError(RULE_NAME, path, e);
4024
}

tools/spectral/ipa/rulesets/functions/IPA109CustomMethodIdentifierFormat.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import {
2-
collectAdoption,
3-
collectAndReturnViolation,
4-
collectException,
5-
handleInternalError,
6-
} from './utils/collectionUtils.js';
1+
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
72
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
8-
import { hasException } from './utils/exceptions.js';
93

104
const RULE_NAME = 'xgen-IPA-109-custom-method-identifier-format';
115

@@ -25,24 +19,21 @@ export default (input, _, { path }) => {
2519
return;
2620
}
2721

28-
if (hasException(input, RULE_NAME)) {
29-
collectException(input, RULE_NAME, path);
30-
return;
31-
}
32-
3322
const errors = checkViolationsAndReturnErrors(pathKey, path);
34-
if (errors.length !== 0) {
35-
return collectAndReturnViolation(path, RULE_NAME, errors);
36-
}
37-
collectAdoption(path, RULE_NAME);
23+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3824
};
3925

4026
function checkViolationsAndReturnErrors(pathKey, path) {
4127
try {
4228
// Check for multiple colons
4329
const colonCount = (pathKey.match(/:/g) || []).length;
4430
if (colonCount > 1) {
45-
return [{ path, message: `Multiple colons found in "${pathKey}".` }];
31+
return [
32+
{
33+
path,
34+
message: `Multiple colons found in "${pathKey}".`,
35+
},
36+
];
4637
}
4738

4839
// Check for slash before colon

tools/spectral/ipa/rulesets/functions/IPA109EachCustomMethodMustBeGetOrPost.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
2-
import { hasException } from './utils/exceptions.js';
3-
import {
4-
collectAdoption,
5-
collectAndReturnViolation,
6-
collectException,
7-
handleInternalError,
8-
} from './utils/collectionUtils.js';
2+
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
93

104
const RULE_NAME = 'xgen-IPA-109-custom-method-must-be-GET-or-POST';
115
const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.';
@@ -20,16 +14,8 @@ export default (input, _, { path }) => {
2014
return;
2115
}
2216

23-
if (hasException(input, RULE_NAME)) {
24-
collectException(input, RULE_NAME, path);
25-
return;
26-
}
27-
2817
const errors = checkViolationsAndReturnErrors(input, path);
29-
if (errors.length !== 0) {
30-
return collectAndReturnViolation(path, RULE_NAME, errors);
31-
}
32-
collectAdoption(path, RULE_NAME);
18+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3319
};
3420

3521
function checkViolationsAndReturnErrors(input, path) {

tools/spectral/ipa/rulesets/functions/IPA109EachCustomMethodMustUseCamelCase.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { getCustomMethodName, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
2-
import { hasException } from './utils/exceptions.js';
32
import { casing } from '@stoplight/spectral-functions';
4-
import {
5-
collectAdoption,
6-
collectAndReturnViolation,
7-
collectException,
8-
handleInternalError,
9-
} from './utils/collectionUtils.js';
3+
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
104

115
const RULE_NAME = 'xgen-IPA-109-custom-method-must-use-camel-case';
126

@@ -18,16 +12,8 @@ export default (input, opts, { path }) => {
1812
return;
1913
}
2014

21-
if (hasException(input, RULE_NAME)) {
22-
collectException(input, RULE_NAME, path);
23-
return;
24-
}
25-
2615
const errors = checkViolationsAndReturnErrors(pathKey, path);
27-
if (errors.length !== 0) {
28-
return collectAndReturnViolation(path, RULE_NAME, errors);
29-
}
30-
collectAdoption(path, RULE_NAME);
16+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3117
};
3218

3319
function checkViolationsAndReturnErrors(pathKey, path) {

tools/spectral/ipa/rulesets/functions/IPA109ValidOperationID.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import { hasException } from './utils/exceptions.js';
2-
import {
3-
collectAdoption,
4-
collectException,
5-
collectAndReturnViolation,
6-
handleInternalError,
7-
} from './utils/collectionUtils.js';
1+
import { handleInternalError, evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
82
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
93
import { hasCustomMethodOverride, VERB_OVERRIDE_EXTENSION, hasVerbOverride } from './utils/extensions.js';
104
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
@@ -18,11 +12,6 @@ export default (input, _, { path }) => {
1812
return;
1913
}
2014

21-
if (hasException(input, RULE_NAME)) {
22-
collectException(input, RULE_NAME, path);
23-
return;
24-
}
25-
2615
let methodName;
2716
let endpointUrl = resourcePath;
2817

@@ -40,12 +29,7 @@ export default (input, _, { path }) => {
4029
}
4130

4231
const errors = validateOperationIdAndReturnErrors(methodName, endpointUrl, input, path);
43-
44-
if (errors.length !== 0) {
45-
return collectAndReturnViolation(path, RULE_NAME, errors);
46-
}
47-
48-
collectAdoption(path, RULE_NAME);
32+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
4933
} catch (e) {
5034
return handleInternalError(RULE_NAME, path, e);
5135
}
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { hasException } from './utils/exceptions.js';
2-
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
1+
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
32
import {
43
getResourcePathItems,
54
isResourceCollectionIdentifier,
@@ -20,17 +19,6 @@ export default (input, _, { path, documentInventory }) => {
2019
return;
2120
}
2221

23-
// Check for exception
24-
if (hasException(input, RULE_NAME)) {
25-
collectException(input, RULE_NAME, path);
26-
return;
27-
}
28-
2922
const errors = checkPaginationQueryParameterAndReturnErrors(input, path, 'itemsPerPage', 100, RULE_NAME);
30-
31-
if (errors.length > 0) {
32-
return collectAndReturnViolation(path, RULE_NAME, errors);
33-
}
34-
35-
collectAdoption(path, RULE_NAME);
23+
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
3624
};

0 commit comments

Comments
 (0)