Skip to content

Commit

Permalink
added validation in alert creation (#807)
Browse files Browse the repository at this point in the history
check for duplicates and error if an alert with same 
name exists

Signed-off-by: Nitish Tiwari <nitish@parseable.com>
Co-authored-by: Nitish Tiwari <nitish@parseable.com>
  • Loading branch information
nikhilsinhaparseable and nitisht authored Jun 7, 2024
1 parent a279822 commit b92c77b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit b92c77b

Please sign in to comment.