MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / validate_enum_value

Function validate_enum_value

crates/openshell-ocsf/src/validation/schema.rs:62–76  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

60///
61/// Checks the `enum` map in the schema attribute definition.
62pub 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)]
79mod tests {

Callers 1

Calls 1

getMethod · 0.45

Tested by 1