Skip to content

Commit 3ab740a

Browse files
fix: all collect helpers in collectionUtils file
1 parent 695af05 commit 3ab740a

File tree

8 files changed

+51
-28
lines changed

8 files changed

+51
-28
lines changed

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

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

59
const RULE_NAME = 'xgen-IPA-109-custom-method-must-be-GET-or-POST';
610
const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.';

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { getCustomMethodName, isCustomMethod } from './utils/resourceEvaluation.js';
2-
import { collectException, hasException } from './utils/exceptions.js';
2+
import { hasException } from './utils/exceptions.js';
33
import { casing } from '@stoplight/spectral-functions';
4-
import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js';
4+
import {
5+
collectAdoption,
6+
collectAndReturnViolation,
7+
collectException,
8+
} from './utils/collectionUtils.js';
59

610
const RULE_NAME = 'xgen-IPA-109-custom-method-must-use-camel-case';
711

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { collectException, hasException } from './utils/exceptions.js';
1+
import { hasException } from './utils/exceptions.js';
22
import { resolveObject } from './utils/componentUtils.js';
33
import { casing } from '@stoplight/spectral-functions';
4-
import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js';
4+
import {
5+
collectAdoption,
6+
collectAndReturnViolation,
7+
collectException,
8+
} from './utils/collectionUtils.js';
59

610
const RULE_NAME = 'xgen-IPA-123-enum-values-must-be-upper-snake-case';
711
const ERROR_MESSAGE = 'enum value must be UPPER_SNAKE_CASE.';

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { isPathParam } from './utils/componentUtils.js';
2-
import { collectException, hasException } from './utils/exceptions.js';
3-
import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js';
2+
import { hasException } from './utils/exceptions.js';
3+
import {
4+
collectAdoption,
5+
collectAndReturnViolation,
6+
collectException,
7+
} from './utils/collectionUtils.js';
48

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import {
66
isSingletonResource,
77
getResourcePaths,
88
} from './utils/resourceEvaluation.js';
9-
import { collectException, hasException } from './utils/exceptions.js';
10-
import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js';
9+
import { hasException } from './utils/exceptions.js';
10+
import {
11+
collectAdoption,
12+
collectAndReturnViolation,
13+
collectException,
14+
} from './utils/collectionUtils.js';
1115

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import {
55
isCustomMethod,
66
isSingletonResource,
77
} from './utils/resourceEvaluation.js';
8-
import { collectException, hasException } from './utils/exceptions.js';
8+
import { hasException } from './utils/exceptions.js';
99
import { getAllSuccessfulGetResponseSchemas } from './utils/methodUtils.js';
10-
import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js';
10+
import {
11+
collectAdoption,
12+
collectAndReturnViolation,
13+
collectException,
14+
} from './utils/collectionUtils.js';
1115

1216
const RULE_NAME = 'xgen-IPA-113-singleton-must-not-have-id';
1317
const ERROR_MESSAGE = 'Singleton resources must not have a user-provided or system-generated ID.';

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ export function collectAndReturnViolation(path, ruleName, errorData) {
3131
export function collectAdoption(path, ruleName) {
3232
collector.add(EntryType.ADOPTION, path, ruleName);
3333
}
34+
35+
/**
36+
* Collects an exception entry.
37+
*
38+
* @param object the object to evaluate
39+
* @param {string} path - The JSON path for the object where the rule exception occurred.
40+
* @param {string} ruleName - The name of the rule that the exception is defined for.
41+
*/
42+
export function collectException(object, ruleName, path) {
43+
const EXCEPTION_EXTENSION = 'x-xgen-IPA-exception';
44+
let exceptionReason = object[EXCEPTION_EXTENSION][ruleName];
45+
if (exceptionReason) {
46+
collector.add(EntryType.EXCEPTION, path, ruleName, exceptionReason);
47+
}
48+
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import collector, { EntryType } from '../../../metrics/collector.js';
2-
31
const EXCEPTION_EXTENSION = 'x-xgen-IPA-exception';
42

53
/**
@@ -15,17 +13,3 @@ export function hasException(object, ruleName) {
1513
}
1614
return false;
1715
}
18-
19-
/**
20-
* Collects an exception entry.
21-
*
22-
* @param object the object to evaluate
23-
* @param {string} path - The JSON path for the object where the rule exception occurred.
24-
* @param {string} ruleName - The name of the rule that the exception is defined for.
25-
*/
26-
export function collectException(object, ruleName, path) {
27-
let exceptionReason = object[EXCEPTION_EXTENSION][ruleName];
28-
if (exceptionReason) {
29-
collector.add(EntryType.EXCEPTION, path, ruleName, exceptionReason);
30-
}
31-
}

0 commit comments

Comments
 (0)