Validate that an enum field in the event has a valid value per the schema. Checks the `enum` map in the schema attribute definition.
(event: &Value, field: &str, schema: &Value)
| 60 | /// |
| 61 | /// Checks the `enum` map in the schema attribute definition. |
| 62 | pub fn validate_enum_value(event: &Value, field: &str, schema: &Value) { |
| 63 | if let Some(val) = event.get(field) |
| 64 | && let Some(attrs) = schema.get("attributes").and_then(|a| a.as_object()) |
| 65 | && let Some(def) = attrs.get(field) |
| 66 | && let Some(enum_map) = def.get("enum").and_then(|e| e.as_object()) |
| 67 | { |
| 68 | let key = val.to_string(); |
| 69 | let key = key.trim_matches('"'); |
| 70 | assert!( |
| 71 | enum_map.contains_key(key), |
| 72 | "Invalid enum value {val} for field '{field}'. Valid: {:?}", |
| 73 | enum_map.keys().collect::<Vec<_>>() |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[cfg(test)] |
| 79 | mod tests { |