Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 7 additions & 51 deletions tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ testRule('xgen-IPA-005-exception-extension-format', [
paths: {
'/path': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {
reason: 'Exception',
},
'xgen-IPA-100-rule-name': 'Exception',
},
},
'/nested': {
post: {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {
reason: 'Exception',
},
'xgen-IPA-100-rule-name': 'Exception',
},
},
},
Expand All @@ -34,40 +30,18 @@ testRule('xgen-IPA-005-exception-extension-format', [
'x-xgen-IPA-exception': 'Exception',
},
'/path2': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': 'Exception',
},
},
'/path3': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {
reason: '',
},
},
},
'/path4': {
'x-xgen-IPA-exception': {
'invalid-rule-name': {
reason: 'Exception',
},
},
},
'/path5': {
'/path3': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {
wrongKey: 'Exception',
},
'invalid-rule-name': 'Exception',
},
},
'/path6': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {
reason: 'Exception',
excessKey: 'Exception',
},
},
},
'/path7': {
'/path4': {
'x-xgen-IPA-exception': {
'xgen-IPA-100-rule-name': {},
},
Expand All @@ -90,31 +64,13 @@ testRule('xgen-IPA-005-exception-extension-format', [
{
code: 'xgen-IPA-005-exception-extension-format',
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
path: ['paths', '/path3', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-005-exception-extension-format',
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
path: ['paths', '/path4', 'x-xgen-IPA-exception', 'invalid-rule-name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-005-exception-extension-format',
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
path: ['paths', '/path5', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-005-exception-extension-format',
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
path: ['paths', '/path6', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
path: ['paths', '/path3', 'x-xgen-IPA-exception', 'invalid-rule-name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-005-exception-extension-format',
message: 'IPA exceptions must have a valid rule name and a reason. http://go/ipa/5',
path: ['paths', '/path7', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
path: ['paths', '/path4', 'x-xgen-IPA-exception', 'xgen-IPA-100-rule-name'],
severity: DiagnosticSeverity.Warning,
},
],
Expand Down
24 changes: 6 additions & 18 deletions tools/spectral/ipa/__tests__/utils/exceptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,35 @@ const TEST_RULE_NAME_100 = 'xgen-IPA-100';

const objectWithIpa100Exception = {
'x-xgen-IPA-exception': {
'xgen-IPA-100': {
reason: 'test',
},
'xgen-IPA-100': 'reason',
},
};

const objectWithNestedIpa100Exception = {
get: {
'x-xgen-IPA-exception': {
'xgen-IPA-100': {
reason: 'test',
},
'xgen-IPA-100': 'reason',
},
},
};

const objectWithIpa100ExceptionAndOwnerExtension = {
'x-xgen-IPA-exception': {
'xgen-IPA-100': {
reason: 'test',
},
'xgen-IPA-100': 'reason',
},
'x-xgen-owner-team': 'apix',
};

const objectWithIpa101Exception = {
'x-xgen-IPA-exception': {
'xgen-IPA-101': {
reason: 'test',
},
'xgen-IPA-101': 'reason',
},
};

const objectWithIpa100And101Exception = {
'x-xgen-IPA-exception': {
'xgen-IPA-101': {
reason: 'test',
},
'xgen-IPA-100': {
reason: 'test',
},
'xgen-IPA-101': 'reason',
'xgen-IPA-100': 'reason',
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const ERROR_MESSAGE = 'IPA exceptions must have a valid rule name and a reason.';
const RULE_NAME_PREFIX = 'xgen-IPA-';
const REASON_KEY = 'reason';

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

exemptedRules.forEach((ruleName) => {
const exception = input[ruleName];
if (!isValidException(ruleName, exception)) {
const reason = input[ruleName];
if (!isValidException(ruleName, reason)) {
errors.push({
path: path.concat([ruleName]),
message: ERROR_MESSAGE,
Expand All @@ -20,12 +19,6 @@ export default (input, _, { path }) => {
return errors;
};

function isValidException(ruleName, exception) {
const exceptionObjectKeys = Object.keys(exception);
return (
ruleName.startsWith(RULE_NAME_PREFIX) &&
exceptionObjectKeys.length === 1 &&
exceptionObjectKeys.includes(REASON_KEY) &&
exception[REASON_KEY] !== ''
);
function isValidException(ruleName, reason) {
return ruleName.startsWith(RULE_NAME_PREFIX) && typeof reason === 'string' && reason !== '';
}
Loading