Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,25 @@ export default (input, { propertyNameToLookFor, cloudProviderEnumValues }, { pat
const propertyObject = resolveObject(oas, path);
const fieldType = path[path.length - 2];

if (fieldType === 'parameters' && !propertyObject.schema) {
return;
}

if (hasException(propertyObject, RULE_NAME)) {
collectException(propertyObject, RULE_NAME, path);
return;
}

const errors = checkViolationsAndReturnErrors(
const result = checkViolationsAndReturnErrors(
input,
propertyObject,
path,
propertyNameToLookFor,
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(
Expand All @@ -46,38 +44,49 @@ 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) {
const enumValues = propertyObject.enum;
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) {
Expand All @@ -86,18 +95,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);
}
Expand Down
Loading