Skip to content

Commit 942b291

Browse files
CLOUDP-288860: Update IPA exception format
1 parent 01ef9ab commit 942b291

File tree

3 files changed

+17
-80
lines changed

3 files changed

+17
-80
lines changed

tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ testRule('xgen-IPA-005-exception-extension-format', [
88
paths: {
99
'/path': {
1010
'x-xgen-IPA-exception': {
11-
'xgen-IPA-100-rule-name': {
12-
reason: 'Exception',
13-
},
11+
'xgen-IPA-100-rule-name': 'Exception',
1412
},
1513
},
1614
'/nested': {
1715
post: {
1816
'x-xgen-IPA-exception': {
19-
'xgen-IPA-100-rule-name': {
20-
reason: 'Exception',
21-
},
17+
'xgen-IPA-100-rule-name': 'Exception',
2218
},
2319
},
2420
},
@@ -34,40 +30,18 @@ testRule('xgen-IPA-005-exception-extension-format', [
3430
'x-xgen-IPA-exception': 'Exception',
3531
},
3632
'/path2': {
37-
'x-xgen-IPA-exception': {
38-
'xgen-IPA-100-rule-name': 'Exception',
39-
},
40-
},
41-
'/path3': {
4233
'x-xgen-IPA-exception': {
4334
'xgen-IPA-100-rule-name': {
4435
reason: '',
4536
},
4637
},
4738
},
48-
'/path4': {
49-
'x-xgen-IPA-exception': {
50-
'invalid-rule-name': {
51-
reason: 'Exception',
52-
},
53-
},
54-
},
55-
'/path5': {
39+
'/path3': {
5640
'x-xgen-IPA-exception': {
57-
'xgen-IPA-100-rule-name': {
58-
wrongKey: 'Exception',
59-
},
41+
'invalid-rule-name': 'Exception',
6042
},
6143
},
62-
'/path6': {
63-
'x-xgen-IPA-exception': {
64-
'xgen-IPA-100-rule-name': {
65-
reason: 'Exception',
66-
excessKey: 'Exception',
67-
},
68-
},
69-
},
70-
'/path7': {
44+
'/path4': {
7145
'x-xgen-IPA-exception': {
7246
'xgen-IPA-100-rule-name': {},
7347
},
@@ -90,31 +64,13 @@ testRule('xgen-IPA-005-exception-extension-format', [
9064
{
9165
code: 'xgen-IPA-005-exception-extension-format',
9266
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
93-
path: ['paths', '/path3', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
94-
severity: DiagnosticSeverity.Warning,
95-
},
96-
{
97-
code: 'xgen-IPA-005-exception-extension-format',
98-
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
99-
path: ['paths', '/path4', 'x-xgen-IPA-exception', 'invalid-rule-name'],
100-
severity: DiagnosticSeverity.Warning,
101-
},
102-
{
103-
code: 'xgen-IPA-005-exception-extension-format',
104-
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
105-
path: ['paths', '/path5', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
106-
severity: DiagnosticSeverity.Warning,
107-
},
108-
{
109-
code: 'xgen-IPA-005-exception-extension-format',
110-
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
111-
path: ['paths', '/path6', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
67+
path: ['paths', '/path3', 'x-xgen-IPA-exception', 'invalid-rule-name'],
11268
severity: DiagnosticSeverity.Warning,
11369
},
11470
{
11571
code: 'xgen-IPA-005-exception-extension-format',
11672
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
117-
path: ['paths', '/path7', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
73+
path: ['paths', '/path4', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
11874
severity: DiagnosticSeverity.Warning,
11975
},
12076
],

tools/spectral/ipa/__tests__/utils/exceptions.test.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,35 @@ const TEST_RULE_NAME_100 = 'xgen-IPA-100';
55

66
const objectWithIpa100Exception = {
77
'x-xgen-IPA-exception': {
8-
'xgen-IPA-100': {
9-
reason: 'test',
10-
},
8+
'xgen-IPA-100': 'reason',
119
},
1210
};
1311

1412
const objectWithNestedIpa100Exception = {
1513
get: {
1614
'x-xgen-IPA-exception': {
17-
'xgen-IPA-100': {
18-
reason: 'test',
19-
},
15+
'xgen-IPA-100': 'reason',
2016
},
2117
},
2218
};
2319

2420
const objectWithIpa100ExceptionAndOwnerExtension = {
2521
'x-xgen-IPA-exception': {
26-
'xgen-IPA-100': {
27-
reason: 'test',
28-
},
22+
'xgen-IPA-100': 'reason',
2923
},
3024
'x-xgen-owner-team': 'apix',
3125
};
3226

3327
const objectWithIpa101Exception = {
3428
'x-xgen-IPA-exception': {
35-
'xgen-IPA-101': {
36-
reason: 'test',
37-
},
29+
'xgen-IPA-101': 'reason',
3830
},
3931
};
4032

4133
const objectWithIpa100And101Exception = {
4234
'x-xgen-IPA-exception': {
43-
'xgen-IPA-101': {
44-
reason: 'test',
45-
},
46-
'xgen-IPA-100': {
47-
reason: 'test',
48-
},
35+
'xgen-IPA-101': 'reason',
36+
'xgen-IPA-100': 'reason',
4937
},
5038
};
5139

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const ERROR_MESSAGE = 'IPA exceptions must have a valid rule name and a reason.';
22
const RULE_NAME_PREFIX = 'xgen-IPA-';
3-
const REASON_KEY = 'reason';
43

54
// Note: This rule does not allow exceptions
65
export default (input, _, { path }) => {
76
const exemptedRules = Object.keys(input);
87
const errors = [];
98

109
exemptedRules.forEach((ruleName) => {
11-
const exception = input[ruleName];
12-
if (!isValidException(ruleName, exception)) {
10+
const reason = input[ruleName];
11+
if (!isValidException(ruleName, reason)) {
1312
errors.push({
1413
path: path.concat([ruleName]),
1514
message: ERROR_MESSAGE,
@@ -20,12 +19,6 @@ export default (input, _, { path }) => {
2019
return errors;
2120
};
2221

23-
function isValidException(ruleName, exception) {
24-
const exceptionObjectKeys = Object.keys(exception);
25-
return (
26-
ruleName.startsWith(RULE_NAME_PREFIX) &&
27-
exceptionObjectKeys.length === 1 &&
28-
exceptionObjectKeys.includes(REASON_KEY) &&
29-
exception[REASON_KEY] !== ''
30-
);
22+
function isValidException(ruleName, reason) {
23+
return ruleName.startsWith(RULE_NAME_PREFIX) && typeof reason === 'string' && reason !== '';
3124
}

0 commit comments

Comments
 (0)