Skip to content

Commit ad1a03b

Browse files
fix(ipa): use rule name from function input instead of hard coded name
1 parent a6756dd commit ad1a03b

File tree

71 files changed

+404
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+404
-391
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { evaluateAndCollectAdoptionStatusWithoutExceptions, handleInternalError } from './utils/collectionUtils.js';
22

3-
const RULE_NAME = 'xgen-IPA-005-exception-extension-format';
43
const ERROR_MESSAGE = 'IPA exceptions must have a valid rule name and a reason.';
54
const RULE_NAME_PREFIX = 'xgen-IPA-';
65

76
// Note: This rule does not allow exceptions
8-
export default (input, _, { path }) => {
9-
const errors = checkViolationsAndReturnErrors(input, path);
10-
return evaluateAndCollectAdoptionStatusWithoutExceptions(errors, RULE_NAME, path);
7+
export default (input, _, { path, rule }) => {
8+
const ruleName = rule.name;
9+
const errors = checkViolationsAndReturnErrors(input, path, ruleName);
10+
return evaluateAndCollectAdoptionStatusWithoutExceptions(errors, ruleName, path);
1111
};
1212

1313
function isValidException(ruleName, reason) {
1414
return ruleName.startsWith(RULE_NAME_PREFIX) && typeof reason === 'string' && reason !== '';
1515
}
1616

17-
function checkViolationsAndReturnErrors(input, path) {
17+
function checkViolationsAndReturnErrors(input, path, ruleName) {
1818
const errors = [];
1919
try {
2020
const exemptedRules = Object.keys(input);
@@ -29,6 +29,6 @@ function checkViolationsAndReturnErrors(input, path) {
2929
});
3030
return errors;
3131
} catch (e) {
32-
return handleInternalError(RULE_NAME, path, e);
32+
return handleInternalError(ruleName, path, e);
3333
}
3434
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { isPathParam } from './utils/componentUtils.js';
33
import { casing } from '@stoplight/spectral-functions';
44
import { findExceptionInPathHierarchy } from './utils/exceptions.js';
55

6-
const RULE_NAME = 'xgen-IPA-102-collection-identifier-camelCase';
76
const ERROR_MESSAGE = 'Collection identifiers must be in camelCase.';
87

98
/**
@@ -15,26 +14,27 @@ const ERROR_MESSAGE = 'Collection identifiers must be in camelCase.';
1514
* @param {object} options - Rule configuration options
1615
* @param {object} context - The context object containing the path and documentInventory
1716
*/
18-
export default (input, options, { path, documentInventory }) => {
17+
export default (input, options, { path, documentInventory, rule }) => {
18+
const ruleName = rule.name;
1919
const oas = documentInventory.resolved;
2020
const pathKey = input;
2121

2222
// Extract ignored values from options
2323
const ignoredValues = options?.ignoredValues || [];
2424

25-
const violations = checkViolations(pathKey, path, ignoredValues);
25+
const violations = checkViolations(pathKey, path, ignoredValues, ruleName);
2626

2727
// Check for exceptions in path hierarchy
28-
const result = findExceptionInPathHierarchy(oas, pathKey, RULE_NAME, path);
28+
const result = findExceptionInPathHierarchy(oas, pathKey, ruleName, path);
2929
if (result?.error) {
3030
return result.error;
3131
}
3232
const objectToCheckForException = result ? oas.paths[result.parentPath] : oas.paths[input];
3333

34-
return evaluateAndCollectAdoptionStatus(violations, RULE_NAME, objectToCheckForException, path);
34+
return evaluateAndCollectAdoptionStatus(violations, ruleName, objectToCheckForException, path);
3535
};
3636

37-
function checkViolations(pathKey, path, ignoredValues = []) {
37+
function checkViolations(pathKey, path, ignoredValues = [], ruleName) {
3838
const violations = [];
3939
try {
4040
// Don't filter out empty segments - they should be treated as violations
@@ -105,9 +105,8 @@ function checkViolations(pathKey, path, ignoredValues = []) {
105105
});
106106
}
107107
});
108+
return violations;
108109
} catch (e) {
109-
return handleInternalError(RULE_NAME, [...path, pathKey], e);
110+
return handleInternalError(ruleName, [...path, pathKey], e);
110111
}
111-
112-
return violations;
113112
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
22
import { findExceptionInPathHierarchy } from './utils/exceptions.js';
33

4-
const RULE_NAME = 'xgen-IPA-102-collection-identifier-pattern';
54
const ERROR_MESSAGE =
65
'Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers (/[a-z][a-zA-Z0-9]*/).';
76
const VALID_IDENTIFIER_PATTERN = /^[a-z][a-zA-Z0-9]*$/;
@@ -15,19 +14,20 @@ const VALID_IDENTIFIER_PATTERN = /^[a-z][a-zA-Z0-9]*$/;
1514
* @param {object} _ - Unused
1615
* @param {object} context - The context object containing the path and documentInventory
1716
*/
18-
export default (input, _, { path, documentInventory }) => {
17+
export default (input, _, { path, documentInventory, rule }) => {
18+
const ruleName = rule.name;
1919
const oas = documentInventory.resolved;
2020

2121
const violations = checkViolations(input, path);
2222

2323
// Check for exceptions in path hierarchy
24-
const result = findExceptionInPathHierarchy(oas, input, RULE_NAME, path);
24+
const result = findExceptionInPathHierarchy(oas, input, ruleName, path);
2525
if (result?.error) {
2626
return result.error;
2727
}
2828
const objectToCheckForException = result ? oas.paths[result.parentPath] : oas.paths[input];
2929

30-
return evaluateAndCollectAdoptionStatus(violations, RULE_NAME, objectToCheckForException, path);
30+
return evaluateAndCollectAdoptionStatus(violations, ruleName, objectToCheckForException, path);
3131
};
3232

3333
function checkViolations(pathKey, path) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/c
33
import { AUTH_PREFIX, UNAUTH_PREFIX } from './utils/resourceEvaluation.js';
44
import { findExceptionInPathHierarchy } from './utils/exceptions.js';
55

6-
const RULE_NAME = 'xgen-IPA-102-path-alternate-resource-name-path-param';
76
const ERROR_MESSAGE = 'API paths must alternate between resource name and path params.';
87

98
const getPrefix = (path) => {
@@ -32,7 +31,8 @@ const validatePathStructure = (elements) => {
3231
* @param {object} _ - Unused
3332
* @param {object} context - The context object containing the path and documentInventory
3433
*/
35-
export default (input, _, { path, documentInventory }) => {
34+
export default (input, _, { path, documentInventory, rule }) => {
35+
const ruleName = rule.name;
3636
const oas = documentInventory.resolved;
3737

3838
const prefix = getPrefix(input);
@@ -45,20 +45,20 @@ export default (input, _, { path, documentInventory }) => {
4545
return;
4646
}
4747

48-
const errors = checkViolationsAndReturnErrors(suffixWithLeadingSlash, path);
48+
const errors = checkViolationsAndReturnErrors(suffixWithLeadingSlash, path, ruleName);
4949

5050
// Check for exceptions in path hierarchy
51-
const result = findExceptionInPathHierarchy(oas, input, RULE_NAME, path);
51+
const result = findExceptionInPathHierarchy(oas, input, ruleName, path);
5252
if (result?.error) {
5353
return result.error;
5454
}
5555

5656
const objectToCheckForException = result ? oas.paths[result.parentPath] : oas.paths[input];
5757

58-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, objectToCheckForException, path);
58+
return evaluateAndCollectAdoptionStatus(errors, ruleName, objectToCheckForException, path);
5959
};
6060

61-
function checkViolationsAndReturnErrors(suffixWithLeadingSlash, path) {
61+
function checkViolationsAndReturnErrors(suffixWithLeadingSlash, path, ruleName) {
6262
try {
6363
let suffix = suffixWithLeadingSlash.slice(1);
6464
let elements = suffix.split('/');
@@ -67,6 +67,6 @@ function checkViolationsAndReturnErrors(suffixWithLeadingSlash, path) {
6767
}
6868
return [];
6969
} catch (e) {
70-
return handleInternalError(RULE_NAME, path, e);
70+
return handleInternalError(ruleName, path, e);
7171
}
7272
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import {
77
} from './utils/resourceEvaluation.js';
88
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js';
99

10-
const RULE_NAME = 'xgen-IPA-104-resource-has-GET';
1110
const ERROR_MESSAGE = 'APIs must provide a get method for resources.';
1211

13-
export default (input, _, { path, documentInventory }) => {
12+
export default (input, _, { path, documentInventory, rule }) => {
13+
const ruleName = rule.name;
1414
if (!isResourceCollectionIdentifier(input)) {
1515
return;
1616
}
1717

1818
const oas = documentInventory.resolved;
1919

20-
const errors = checkViolationsAndReturnErrors(oas.paths, input, path);
20+
const errors = checkViolationsAndReturnErrors(oas.paths, input, path, ruleName);
2121

22-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, oas.paths[input], path);
22+
return evaluateAndCollectAdoptionStatus(errors, ruleName, oas.paths[input], path);
2323
};
2424

25-
function checkViolationsAndReturnErrors(oasPaths, input, path) {
25+
function checkViolationsAndReturnErrors(oasPaths, input, path, ruleName) {
2626
try {
2727
const resourcePathItems = getResourcePathItems(input, oasPaths);
2828
const resourcePaths = Object.keys(resourcePathItems);
@@ -42,6 +42,6 @@ function checkViolationsAndReturnErrors(oasPaths, input, path) {
4242
}
4343
return [];
4444
} catch (e) {
45-
return handleInternalError(RULE_NAME, path, e);
45+
return handleInternalError(ruleName, path, e);
4646
}
4747
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
isSingletonResource,
77
} from './utils/resourceEvaluation.js';
88

9-
const RULE_NAME = 'xgen-IPA-104-get-method-no-request-body';
109
const ERROR_MESSAGE = 'The Get method must not include a request body.';
1110

12-
export default (input, _, { path, documentInventory }) => {
11+
export default (input, _, { path, documentInventory, rule }) => {
12+
const ruleName = rule.name;
1313
const resourcePath = path[1];
1414
const oas = documentInventory.resolved;
1515

@@ -24,7 +24,7 @@ export default (input, _, { path, documentInventory }) => {
2424

2525
const errors = checkViolationsAndReturnErrors(input, path);
2626

27-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
27+
return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path);
2828
};
2929

3030
function checkViolationsAndReturnErrors(getOperationObject, path) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
import { resolveObject } from './utils/componentUtils.js';
99
import { checkForbiddenPropertyAttributesAndReturnErrors } from './utils/validations/checkForbiddenPropertyAttributesAndReturnErrors.js';
1010

11-
const RULE_NAME = 'xgen-IPA-104-get-method-response-has-no-input-fields';
1211
const ERROR_MESSAGE = 'The get method response object must not include output fields (writeOnly properties).';
1312

14-
export default (input, _, { path, documentInventory }) => {
13+
export default (input, _, { path, documentInventory, rule }) => {
14+
const ruleName = rule.name;
1515
const resourcePath = path[1];
1616
const responseCode = path[4];
1717
const oas = documentInventory.resolved;
@@ -37,5 +37,5 @@ export default (input, _, { path, documentInventory }) => {
3737
ERROR_MESSAGE
3838
);
3939

40-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, contentPerMediaType, path);
40+
return evaluateAndCollectAdoptionStatus(errors, ruleName, contentPerMediaType, path);
4141
};

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import {
2-
isSingleResourceIdentifier,
3-
isSingletonResource,
42
getResourcePathItems,
53
isResourceCollectionIdentifier,
4+
isSingleResourceIdentifier,
5+
isSingletonResource,
66
} from './utils/resourceEvaluation.js';
77
import { resolveObject } from './utils/componentUtils.js';
88
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js';
99
import { checkSchemaRefSuffixAndReturnErrors } from './utils/validations/checkSchemaRefSuffixAndReturnErrors.js';
1010

11-
const RULE_NAME = 'xgen-IPA-104-get-method-returns-response-suffixed-object';
12-
13-
export default (input, _, { path, documentInventory }) => {
11+
export default (input, _, { path, documentInventory, rule }) => {
12+
const ruleName = rule.name;
1413
const resourcePath = path[1];
1514
const responseCode = path[4];
1615
const oas = documentInventory.unresolved;
@@ -28,7 +27,7 @@ export default (input, _, { path, documentInventory }) => {
2827
return;
2928
}
3029

31-
const errors = checkSchemaRefSuffixAndReturnErrors(path, contentPerMediaType, 'Response', RULE_NAME);
30+
const errors = checkSchemaRefSuffixAndReturnErrors(path, contentPerMediaType, 'Response', ruleName);
3231

33-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, contentPerMediaType, path);
32+
return evaluateAndCollectAdoptionStatus(errors, ruleName, contentPerMediaType, path);
3433
};

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/c
88
import { schemaIsArray, schemaIsPaginated } from './utils/schemaUtils.js';
99
import { resolveObject } from './utils/componentUtils.js';
1010

11-
const RULE_NAME = 'xgen-IPA-104-get-method-returns-single-resource';
1211
const ERROR_MESSAGE_STANDARD_RESOURCE =
1312
'Get methods should return data for a single resource. This method returns an array or a paginated response.';
1413
const ERROR_MESSAGE_SINGLETON_RESOURCE =
1514
'Get methods for singleton resource should return data for a single resource. This method returns an array or a paginated response. If this is not a singleton resource, please implement all standard methods.';
1615

17-
export default (input, _, { path, documentInventory }) => {
16+
export default (input, _, { path, documentInventory, rule }) => {
17+
const ruleName = rule.name;
1818
const oas = documentInventory.resolved;
1919
const resourcePath = path[1];
2020
const responseCode = path[4];
@@ -34,12 +34,12 @@ export default (input, _, { path, documentInventory }) => {
3434
return;
3535
}
3636

37-
const errors = checkViolationsAndReturnErrors(contentPerMediaType, path, isSingleton);
37+
const errors = checkViolationsAndReturnErrors(contentPerMediaType, path, isSingleton, ruleName);
3838

39-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, contentPerMediaType, path);
39+
return evaluateAndCollectAdoptionStatus(errors, ruleName, contentPerMediaType, path);
4040
};
4141

42-
function checkViolationsAndReturnErrors(contentPerMediaType, path, isSingleton) {
42+
function checkViolationsAndReturnErrors(contentPerMediaType, path, isSingleton, ruleName) {
4343
try {
4444
const schema = contentPerMediaType.schema;
4545
if (schemaIsPaginated(schema) || schemaIsArray(schema)) {
@@ -52,6 +52,6 @@ function checkViolationsAndReturnErrors(contentPerMediaType, path, isSingleton)
5252
}
5353
return [];
5454
} catch (e) {
55-
return handleInternalError(RULE_NAME, path, e);
55+
return handleInternalError(ruleName, path, e);
5656
}
5757
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
} from './utils/resourceEvaluation.js';
88
import { checkResponseCodeAndReturnErrors } from './utils/validations/checkResponseCodeAndReturnErrors.js';
99

10-
const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200';
1110
const ERROR_MESSAGE =
1211
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';
1312

14-
export default (input, _, { path, documentInventory }) => {
13+
export default (input, _, { path, documentInventory, rule }) => {
14+
const ruleName = rule.name;
1515
const resourcePath = path[1];
1616
const oas = documentInventory.resolved;
1717
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
@@ -23,7 +23,7 @@ export default (input, _, { path, documentInventory }) => {
2323
return;
2424
}
2525

26-
const errors = checkResponseCodeAndReturnErrors(input, '200', path, RULE_NAME, ERROR_MESSAGE);
26+
const errors = checkResponseCodeAndReturnErrors(input, '200', path, ruleName, ERROR_MESSAGE);
2727

28-
return evaluateAndCollectAdoptionStatus(errors, RULE_NAME, input, path);
28+
return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path);
2929
};

0 commit comments

Comments
 (0)