diff --git a/tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js b/tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js index a73126027a..2f39e0565c 100644 --- a/tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js +++ b/tools/spectral/ipa/__tests__/exceptionExtensionFormat.test.js @@ -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', }, }, }, @@ -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': {}, }, @@ -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, }, ], diff --git a/tools/spectral/ipa/__tests__/utils/exceptions.test.js b/tools/spectral/ipa/__tests__/utils/exceptions.test.js index 92e957d910..6646e861a6 100644 --- a/tools/spectral/ipa/__tests__/utils/exceptions.test.js +++ b/tools/spectral/ipa/__tests__/utils/exceptions.test.js @@ -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', }, }; diff --git a/tools/spectral/ipa/rulesets/functions/exceptionExtensionFormat.js b/tools/spectral/ipa/rulesets/functions/exceptionExtensionFormat.js index 71dfefebdc..0cb71e7c56 100644 --- a/tools/spectral/ipa/rulesets/functions/exceptionExtensionFormat.js +++ b/tools/spectral/ipa/rulesets/functions/exceptionExtensionFormat.js @@ -1,6 +1,5 @@ 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 }) => { @@ -8,8 +7,8 @@ export default (input, _, { path }) => { 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, @@ -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 !== ''; }