Skip to content

Commit

Permalink
fix validation of failure criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
pankhurisaxena28 committed Jul 3, 2024
1 parent 6d3c77c commit 3874145
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ function constructKeyValueMapFromString(str?: string): Map<string, string> {
if (criteria.split(':').length != 2) {
throw new Error(`string format invalid`);
}
const [key, value] = criteria.split(':');
keyValueMap.set(key.trim().toUpperCase(), value.trim().toUpperCase());
let [key, value] = criteria.split(':');
key = key.trim().toUpperCase();
value = value.trim().toUpperCase();
if (keyValueMap.has(key) && key == 'OPERATOR') {
throw new Error(`multiple operators found`);
} else if (keyValueMap.has(key)) {
throw new Error(`multiple severities of type ${key} found`);
}
keyValueMap.set(key, value);
});
return keyValueMap;
}

function validateAndExtractFailureCriteriaFromMap(
keyValueMap: Map<string, string>,
): FailureCriteria {
Expand All @@ -125,19 +131,13 @@ function validateAndExtractFailureCriteriaFromMap(

keyValueMap.forEach((value, key) => {
if (isValidOperatorKey(key)) {
if (operator) {
throw new Error(`multiple operators found`);
}
operator = extractOperatorValue(value);
return;
}
const severity: Severity = extractSeverityKey(
key,
/** errMsg= */ `invalid key: ${key}, value: ${value} pair found`,
);
if (violationsThresholdBySeverity.has(severity)) {
throw new Error(`multiple severities of type ${key} found`);
}
if (isNaN(+value)) {
throw new Error(`invalid severity count`);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ test(
input: 'Low: 1, Operator: OR, Operator: OR',
error: 'failure_criteria validation failed : multiple operators found',
},
{
name: 'multiple operators',
input: 'Low: 1, Operator: AND, Operator: OR',
error: 'failure_criteria validation failed : multiple operators found',
},
{
name: 'no operator',
input: 'Low: 1',
Expand Down

0 comments on commit 3874145

Please sign in to comment.