Skip to content

Commit 792c86b

Browse files
CLOUDP-306576: IPA-119: Multi-Cloud Support (Fix)
1 parent cdc3e8a commit 792c86b

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ testRule('xgen-IPA-119-no-default-for-cloud-providers', [
2121
errors: [],
2222
},
2323
{
24-
name: 'invalid when cloud provider field has default value',
24+
name: 'invalid when cloudProvider field has default value',
2525
document: {
2626
components: {
2727
schemas: {
@@ -47,7 +47,7 @@ testRule('xgen-IPA-119-no-default-for-cloud-providers', [
4747
],
4848
},
4949
{
50-
name: 'invalid when cloud provider field has default value',
50+
name: 'invalid when provider field has default value',
5151
document: {
5252
components: {
5353
schemas: {

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

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,25 @@ export default (input, { propertyNameToLookFor, cloudProviderEnumValues }, { pat
1414
const propertyObject = resolveObject(oas, path);
1515
const fieldType = path[path.length - 2];
1616

17-
if (fieldType === 'parameters' && !propertyObject.schema) {
18-
return;
19-
}
20-
2117
if (hasException(propertyObject, RULE_NAME)) {
2218
collectException(propertyObject, RULE_NAME, path);
2319
return;
2420
}
2521

26-
const errors = checkViolationsAndReturnErrors(
22+
const result = checkViolationsAndReturnErrors(
2723
input,
2824
propertyObject,
2925
path,
3026
propertyNameToLookFor,
3127
fieldType,
3228
cloudProviderEnumValues
3329
);
34-
if (errors.length !== 0) {
35-
return collectAndReturnViolation(path, RULE_NAME, errors);
30+
if (result.errors.length !== 0) {
31+
return collectAndReturnViolation(path, RULE_NAME, result.errors);
32+
}
33+
if (result.isCloudProviderField) {
34+
collectAdoption(path, RULE_NAME);
3635
}
37-
collectAdoption(path, RULE_NAME);
3836
};
3937

4038
function checkViolationsAndReturnErrors(
@@ -46,38 +44,49 @@ function checkViolationsAndReturnErrors(
4644
cloudProviderEnumValues
4745
) {
4846
try {
47+
const result = {
48+
errors: [],
49+
isCloudProviderField: false,
50+
};
51+
4952
if (fieldType === 'properties') {
50-
if (propertyName === propertyNameToLookFor && propertyObject.default !== undefined) {
51-
return [
52-
{
53+
if (propertyName === propertyNameToLookFor) {
54+
result.isCloudProviderField = true;
55+
if (propertyObject.default !== undefined) {
56+
result.errors.push({
5357
path,
5458
message: ERROR_MESSAGE,
55-
},
56-
];
59+
});
60+
return result;
61+
}
5762
}
5863

5964
if (Array.isArray(propertyObject.enum) && propertyObject.enum.length > 0) {
6065
const enumValues = propertyObject.enum;
6166
const hasCloudProviderEnumValue = cloudProviderEnumValues.every((cloudProviderValue) =>
6267
enumValues.includes(cloudProviderValue)
6368
);
64-
if (hasCloudProviderEnumValue && propertyObject.default !== undefined) {
65-
return [
66-
{
69+
if (hasCloudProviderEnumValue) {
70+
result.isCloudProviderField = true;
71+
if (propertyObject.default !== undefined) {
72+
result.errors.push({
6773
path,
6874
message: ERROR_MESSAGE,
69-
},
70-
];
75+
});
76+
return result;
77+
}
7178
}
7279
}
7380
} else if (fieldType === 'parameters') {
74-
if (propertyObject.name === propertyNameToLookFor && propertyObject.schema.default !== undefined) {
75-
return [
76-
{
81+
if (propertyObject.name === propertyNameToLookFor) {
82+
result.isCloudProviderField = true;
83+
if (propertyObject.schema.default !== undefined) {
84+
result.errors.push({
7785
path,
7886
message: ERROR_MESSAGE,
79-
},
80-
];
87+
});
88+
return result;
89+
}
8190
}
8291

8392
if (Array.isArray(propertyObject.schema.enum) && propertyObject.schema.enum.length > 0) {
@@ -86,18 +95,20 @@ function checkViolationsAndReturnErrors(
8695
enumValues.includes(cloudProviderValue)
8796
);
8897

89-
if (hasCloudProviderEnumValue && propertyObject.schema.default !== undefined) {
90-
return [
91-
{
98+
if (hasCloudProviderEnumValue) {
99+
result.isCloudProviderField = true;
100+
if (propertyObject.schema.default !== undefined) {
101+
result.errors.push({
92102
path,
93103
message: ERROR_MESSAGE,
94-
},
95-
];
104+
});
105+
return result;
106+
}
96107
}
97108
}
98109
}
99110

100-
return [];
111+
return result;
101112
} catch (e) {
102113
handleInternalError(RULE_NAME, path, e);
103114
}

0 commit comments

Comments
 (0)