From b92c77bfa95e196c13e6ed557f66663b8e603595 Mon Sep 17 00:00:00 2001 From: Nikhil Sinha <131262146+nikhilsinhaparseable@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:24:35 +0530 Subject: [PATCH] added validation in alert creation (#807) check for duplicates and error if an alert with same name exists Signed-off-by: Nitish Tiwari Co-authored-by: Nitish Tiwari --- server/src/validator.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/src/validator.rs b/server/src/validator.rs index 15bf60c02..db0a7ecab 100644 --- a/server/src/validator.rs +++ b/server/src/validator.rs @@ -38,10 +38,19 @@ const DENIED_NAMES: &[&str] = &[ ]; pub fn alert(alerts: &Alerts) -> Result<(), AlertValidationError> { + let alert_name: Vec<&str> = alerts.alerts.iter().map(|a| a.name.as_str()).collect(); + let mut alert_name_dedup = alert_name.clone(); + alert_name_dedup.sort(); + alert_name_dedup.dedup(); + + if alert_name.len() != alert_name_dedup.len() { + return Err(AlertValidationError::ExistingName); + } for alert in &alerts.alerts { if alert.name.is_empty() { return Err(AlertValidationError::EmptyName); } + if alert.message.message.is_empty() { return Err(AlertValidationError::EmptyMessage); } @@ -145,6 +154,8 @@ pub mod error { pub enum AlertValidationError { #[error("Alert name cannot be empty")] EmptyName, + #[error("Alert with the same name already exists")] + ExistingName, #[error("Alert message cannot be empty")] EmptyMessage, #[error("Alert's rule.column cannot be empty")]