Skip to content

Commit c21986c

Browse files
Fix validation of failure criteria to handle multiple severities and operators (#47)
<!-- Thank you for proposing a pull request! Please note that SOME TESTS WILL LIKELY FAIL due to how GitHub exposes secrets in Pull Requests from forks. Someone from the team will review your Pull Request and respond. Please describe your change and any implementation details below. --> --------- Signed-off-by: pankhurisaxena28 <[email protected]>
1 parent 6d3c77c commit c21986c

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

dist/main/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,15 @@ function constructKeyValueMapFromString(str?: string): Map<string, string> {
111111
if (criteria.split(':').length != 2) {
112112
throw new Error(`string format invalid`);
113113
}
114-
const [key, value] = criteria.split(':');
115-
keyValueMap.set(key.trim().toUpperCase(), value.trim().toUpperCase());
114+
let [key, value] = criteria.split(':');
115+
key = key.trim().toUpperCase();
116+
value = value.trim().toUpperCase();
117+
if (keyValueMap.has(key) && key == 'OPERATOR') {
118+
throw new Error(`multiple operators found`);
119+
} else if (keyValueMap.has(key)) {
120+
throw new Error(`multiple severities of type ${key} found`);
121+
}
122+
keyValueMap.set(key, value);
116123
});
117124
return keyValueMap;
118125
}
@@ -125,19 +132,13 @@ function validateAndExtractFailureCriteriaFromMap(
125132

126133
keyValueMap.forEach((value, key) => {
127134
if (isValidOperatorKey(key)) {
128-
if (operator) {
129-
throw new Error(`multiple operators found`);
130-
}
131135
operator = extractOperatorValue(value);
132136
return;
133137
}
134138
const severity: Severity = extractSeverityKey(
135139
key,
136140
/** errMsg= */ `invalid key: ${key}, value: ${value} pair found`,
137141
);
138-
if (violationsThresholdBySeverity.has(severity)) {
139-
throw new Error(`multiple severities of type ${key} found`);
140-
}
141142
if (isNaN(+value)) {
142143
throw new Error(`invalid severity count`);
143144
}

tests/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ test(
121121
input: 'Low: 1, Operator: OR, Operator: OR',
122122
error: 'failure_criteria validation failed : multiple operators found',
123123
},
124+
{
125+
name: 'multiple operators',
126+
input: 'Low: 1, Operator: AND, Operator: OR',
127+
error: 'failure_criteria validation failed : multiple operators found',
128+
},
124129
{
125130
name: 'no operator',
126131
input: 'Low: 1',

0 commit comments

Comments
 (0)