From f8a269dab606c9d08ec71d684c29e5ea3b79344b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20K=C5=91v=C3=A1ri?= Date: Wed, 17 Jul 2024 16:54:53 +0200 Subject: [PATCH] fix(validatation): deeply nested loops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Márk Kővári --- crates/wadm-types/src/validation.rs | 39 +++++++++++++---------------- tests/validation.rs | 2 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/crates/wadm-types/src/validation.rs b/crates/wadm-types/src/validation.rs index f4d41b95..12e3d343 100644 --- a/crates/wadm-types/src/validation.rs +++ b/crates/wadm-types/src/validation.rs @@ -408,28 +408,23 @@ fn check_dangling_links(manifest: &Manifest) -> Vec { fn check_source_config_on_components(manifest: &Manifest) -> Vec { let forbidden_config_key = "source_config"; let mut failures = Vec::new(); - let components = manifest.components(); - for component in components { - for component_traits in component.traits.iter() { - for component_trait in component_traits { - match &component_trait.properties { - TraitProperty::Custom(custom) => { - match custom { - Value::Object(custom_trait_config) => { - if custom_trait_config.contains_key(forbidden_config_key) { - failures.push(ValidationFailure::new( - ValidationFailureLevel::Error, - format!( - "source_config found on one of the component: {}'s traits properties", - component.name - ), - )) - } - } - _ => {} - } - } - _ => {} + let components = manifest.component_lookup(); + let component_traits = components + .into_iter() + .filter_map(|(name, component)| match &component.traits { + Some(traits) => Some((name, traits)), + None => None, + }); + for (name, traits) in component_traits { + for trait_ in traits { + if let TraitProperty::Custom(custom) = trait_.properties.clone() { + if let Some(_) = custom.get(forbidden_config_key) { + failures.push(ValidationFailure::new( + ValidationFailureLevel::Error, + format!( + "component [{name}] has source_config in one of its traits properties", + ), + )) } } } diff --git a/tests/validation.rs b/tests/validation.rs index 78a04895..dde0b00d 100644 --- a/tests/validation.rs +++ b/tests/validation.rs @@ -139,7 +139,7 @@ async fn validate_source_config_on_component_errors() -> Result<()> { ); assert_eq!( msg, - "source_config found on one of the component: http-component's traits properties", + "component [http-component] has source_config in one of its traits properties", "expected error message, but was incorrect", ); Ok(())