From dff725be5c065cf14111086b8a48d480a432d123 Mon Sep 17 00:00:00 2001 From: Julia Zolotarev Date: Thu, 17 Oct 2024 16:06:47 -0400 Subject: [PATCH] APP-15732 - Add validation for labels field --- scripts/validate.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/scripts/validate.ts b/scripts/validate.ts index c10f0c5..7d12996 100644 --- a/scripts/validate.ts +++ b/scripts/validate.ts @@ -58,6 +58,30 @@ const validateRule = (rule) => { `Rule ${rule.name} has an invalid alertLevel. ${rule.alertLevel} is not a valid alertLevel.` ); } + if (rule.labels) { + if (!Array.isArray(rule.labels)) { + throw new Error( + `Rule ${rule.name} has an invalid labels property. Labels must be an array.` + ); + } + + const labelNameMap = {}; + + for (const label of rule.labels) { + if (!label.labelName || !label.labelValue) { + throw new Error( + `Rule ${rule.name} has an invalid label. "${label}" must include a labelName property and labelValue property.` + ); + } + const normalizedLabelName = label.labelName.trim().toLowerCase(); + if (labelNameMap[normalizedLabelName]) { + throw new Error( + `Rule ${rule.name} has invalid labels. Duplicate label names are not allowed on a rule` + ); + } + labelNameMap[normalizedLabelName] = true; + } + } for (const queryObj of rule.queries) { if (RegExp(/[^A-Za-z0-9_]/g).test(queryObj.name)) { throw new Error(