Skip to content

Commit dc29321

Browse files
author
Sophia Marie Terry
committed
CLOUDP-306294: Refactored IPA109 Validation function + prettier
1 parent a4d1db9 commit dc29321

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
33
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
5-
import { hasMethodWithVerbOverride, hasCustomMethodOverride } from './utils/extensions.js';
5+
import { hasMethodWithVerbOverride, hasCustomMethodOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
66

77
const RULE_NAME = 'xgen-IPA-109-valid-operation-id';
88
const ERROR_MESSAGE = 'Invalid OperationID.';
@@ -14,11 +14,12 @@ export default (input, _, { path }) => {
1414
return;
1515
}
1616

17-
let errors = [];
18-
let expectedOperationID = '';
19-
if (isCustomMethodIdentifier(resourcePath)) {
20-
expectedOperationID = generateOperationID(getCustomMethodName(resourcePath), stripCustomMethodName(resourcePath));
17+
if (hasException(input, RULE_NAME)) {
18+
collectException(input, RULE_NAME, path);
19+
return;
20+
}
2121

22+
if (isCustomMethodIdentifier(resourcePath)) {
2223
let obj;
2324
if (input.post) {
2425
obj = input.post;
@@ -28,12 +29,11 @@ export default (input, _, { path }) => {
2829
return;
2930
}
3031

31-
if (hasException(obj, RULE_NAME)) {
32-
collectException(obj, RULE_NAME, path);
33-
return;
34-
}
35-
3632
const operationId = obj.operationId;
33+
const expectedOperationID = generateOperationID(
34+
getCustomMethodName(resourcePath),
35+
stripCustomMethodName(resourcePath)
36+
);
3737
if (operationId !== expectedOperationID) {
3838
const errors = [
3939
{
@@ -44,27 +44,27 @@ export default (input, _, { path }) => {
4444
return collectAndReturnViolation(path, RULE_NAME, errors);
4545
}
4646
} else if (hasMethodWithVerbOverride(input)) {
47-
const methods = Object.keys(input);
48-
for (let i = 0; i < methods.length; i++) {
49-
let obj = input[methods[i]];
50-
const operationId = obj.operationId;
51-
if (hasCustomMethodOverride(obj)) {
52-
expectedOperationID = generateOperationID(obj['x-xgen-method-verb-override'].verb, resourcePath);
47+
const methods = Object.values(input);
48+
let errors = [];
49+
methods.forEach((method) => {
50+
if (hasCustomMethodOverride(method)) {
51+
const operationId = method.operationId;
52+
const expectedOperationID = generateOperationID(method[VERB_OVERRIDE_EXTENSION].verb, resourcePath);
5353
if (operationId !== expectedOperationID) {
5454
errors.push({
5555
path,
5656
message: `${ERROR_MESSAGE} Found ${operationId}, expected ${expectedOperationID}.`,
5757
});
5858
}
5959
}
60+
});
61+
62+
if (errors.length !== 0) {
63+
return collectAndReturnViolation(path, RULE_NAME, errors);
6064
}
6165
} else {
6266
return;
6367
}
6468

65-
if (errors.length !== 0) {
66-
return collectAndReturnViolation(path, RULE_NAME, errors);
67-
}
68-
6969
collectAdoption(path, RULE_NAME);
7070
};

tools/spectral/ipa/rulesets/functions/utils/extensions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const VERB_OVERRIDE_EXTENSION = 'x-xgen-method-verb-override';
77
* @returns {boolean} true if the endpoint has a nested method with the extension, otherwise false
88
*/
99
export function hasMethodWithVerbOverride(endpoint) {
10-
return Object.values(endpoint).some(hasVerbOverride);
10+
return Object.values(endpoint).some(hasVerbOverride);
1111
}
1212

1313
/**
@@ -27,11 +27,10 @@ export function hasCustomMethodOverride(object) {
2727
* @param verb the verb to inspect the extension for
2828
* @returns {boolean} true if the object has the extension with the given verb, otherwise false
2929
*/
30-
export function hasMethodVerbOverride(object, verb){
31-
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb === verb
30+
export function hasMethodVerbOverride(object, verb) {
31+
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb === verb;
3232
}
3333

34-
3534
/**
3635
* Checks if the object has an extension "x-xgen-method-verb-override"
3736
*

0 commit comments

Comments
 (0)