diff --git a/tools/spectral/ipa/__tests__/IPA119NoDefaultForCloudProviders.test.js b/tools/spectral/ipa/__tests__/IPA119NoDefaultForCloudProviders.test.js index 408b0dd959..c5bee55724 100644 --- a/tools/spectral/ipa/__tests__/IPA119NoDefaultForCloudProviders.test.js +++ b/tools/spectral/ipa/__tests__/IPA119NoDefaultForCloudProviders.test.js @@ -21,7 +21,7 @@ testRule('xgen-IPA-119-no-default-for-cloud-providers', [ errors: [], }, { - name: 'invalid when cloud provider field has default value', + name: 'invalid when cloudProvider field has default value', document: { components: { schemas: { @@ -47,7 +47,7 @@ testRule('xgen-IPA-119-no-default-for-cloud-providers', [ ], }, { - name: 'invalid when cloud provider field has default value', + name: 'invalid when provider field has default value', document: { components: { schemas: { diff --git a/tools/spectral/ipa/rulesets/functions/IPA119NoDefaultForCloudProviders.js b/tools/spectral/ipa/rulesets/functions/IPA119NoDefaultForCloudProviders.js index 032e7c4590..ff6a8d28b5 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA119NoDefaultForCloudProviders.js +++ b/tools/spectral/ipa/rulesets/functions/IPA119NoDefaultForCloudProviders.js @@ -23,7 +23,7 @@ export default (input, { propertyNameToLookFor, cloudProviderEnumValues }, { pat return; } - const errors = checkViolationsAndReturnErrors( + const result = checkViolationsAndReturnErrors( input, propertyObject, path, @@ -31,10 +31,12 @@ export default (input, { propertyNameToLookFor, cloudProviderEnumValues }, { pat fieldType, cloudProviderEnumValues ); - if (errors.length !== 0) { - return collectAndReturnViolation(path, RULE_NAME, errors); + if (result.errors.length !== 0) { + return collectAndReturnViolation(path, RULE_NAME, result.errors); + } + if (result.isCloudProviderField) { + collectAdoption(path, RULE_NAME); } - collectAdoption(path, RULE_NAME); }; function checkViolationsAndReturnErrors( @@ -46,14 +48,21 @@ function checkViolationsAndReturnErrors( cloudProviderEnumValues ) { try { + const result = { + errors: [], + isCloudProviderField: false, + }; + if (fieldType === 'properties') { - if (propertyName === propertyNameToLookFor && propertyObject.default !== undefined) { - return [ - { + if (propertyName === propertyNameToLookFor) { + result.isCloudProviderField = true; + if (propertyObject.default !== undefined) { + result.errors.push({ path, message: ERROR_MESSAGE, - }, - ]; + }); + return result; + } } if (Array.isArray(propertyObject.enum) && propertyObject.enum.length > 0) { @@ -61,23 +70,27 @@ function checkViolationsAndReturnErrors( const hasCloudProviderEnumValue = cloudProviderEnumValues.every((cloudProviderValue) => enumValues.includes(cloudProviderValue) ); - if (hasCloudProviderEnumValue && propertyObject.default !== undefined) { - return [ - { + if (hasCloudProviderEnumValue) { + result.isCloudProviderField = true; + if (propertyObject.default !== undefined) { + result.errors.push({ path, message: ERROR_MESSAGE, - }, - ]; + }); + return result; + } } } } else if (fieldType === 'parameters') { - if (propertyObject.name === propertyNameToLookFor && propertyObject.schema.default !== undefined) { - return [ - { + if (propertyObject.name === propertyNameToLookFor) { + result.isCloudProviderField = true; + if (propertyObject.schema.default !== undefined) { + result.errors.push({ path, message: ERROR_MESSAGE, - }, - ]; + }); + return result; + } } if (Array.isArray(propertyObject.schema.enum) && propertyObject.schema.enum.length > 0) { @@ -86,18 +99,20 @@ function checkViolationsAndReturnErrors( enumValues.includes(cloudProviderValue) ); - if (hasCloudProviderEnumValue && propertyObject.schema.default !== undefined) { - return [ - { + if (hasCloudProviderEnumValue) { + result.isCloudProviderField = true; + if (propertyObject.schema.default !== undefined) { + result.errors.push({ path, message: ERROR_MESSAGE, - }, - ]; + }); + return result; + } } } } - return []; + return result; } catch (e) { handleInternalError(RULE_NAME, path, e); }