Skip to content

Commit b92c77b

Browse files
added validation in alert creation (#807)
check for duplicates and error if an alert with same name exists Signed-off-by: Nitish Tiwari <[email protected]> Co-authored-by: Nitish Tiwari <[email protected]>
1 parent a279822 commit b92c77b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

server/src/validator.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ const DENIED_NAMES: &[&str] = &[
3838
];
3939

4040
pub fn alert(alerts: &Alerts) -> Result<(), AlertValidationError> {
41+
let alert_name: Vec<&str> = alerts.alerts.iter().map(|a| a.name.as_str()).collect();
42+
let mut alert_name_dedup = alert_name.clone();
43+
alert_name_dedup.sort();
44+
alert_name_dedup.dedup();
45+
46+
if alert_name.len() != alert_name_dedup.len() {
47+
return Err(AlertValidationError::ExistingName);
48+
}
4149
for alert in &alerts.alerts {
4250
if alert.name.is_empty() {
4351
return Err(AlertValidationError::EmptyName);
4452
}
53+
4554
if alert.message.message.is_empty() {
4655
return Err(AlertValidationError::EmptyMessage);
4756
}
@@ -145,6 +154,8 @@ pub mod error {
145154
pub enum AlertValidationError {
146155
#[error("Alert name cannot be empty")]
147156
EmptyName,
157+
#[error("Alert with the same name already exists")]
158+
ExistingName,
148159
#[error("Alert message cannot be empty")]
149160
EmptyMessage,
150161
#[error("Alert's rule.column cannot be empty")]

0 commit comments

Comments
 (0)