From 609f7860ca9d6f62b05e0d979656333ae4f2a3e7 Mon Sep 17 00:00:00 2001 From: Eric Rescorla Date: Sun, 1 Dec 2024 17:01:18 -0800 Subject: [PATCH 1/2] Add validation to set_global_property_value. Fixes #107 --- src/global_properties.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/global_properties.rs b/src/global_properties.rs index 6895807..f0d343c 100644 --- a/src/global_properties.rs +++ b/src/global_properties.rs @@ -54,11 +54,10 @@ where Arc::new( |context: &mut Context, name, value| -> Result<(), IxaError> { let val: T::Value = serde_json::from_value(value)?; - T::validate(&val)?; if context.get_global_property_value(T::new()).is_some() { return Err(IxaError::IxaError(format!("Duplicate property {name}"))); } - let _ = context.set_global_property_value(T::new(), val); + context.set_global_property_value(T::new(), val)?; Ok(()) }, ), @@ -225,6 +224,7 @@ impl ContextGlobalPropertiesExt for Context { property: T, value: T::Value, ) -> Result<(), IxaError> { + T::validate(&value)?; let data_container = self.get_data_container_mut(GlobalPropertiesPlugin); data_container.set_global_property_value(&property, value) } @@ -439,8 +439,28 @@ mod test { ))), } }); + + #[test] + fn validate_property_set_success() { + let mut context = Context::new(); + context.set_global_property_value(Property3, Property3Type { + field_int: 0}).unwrap(); + } + + #[test] + fn validate_property_set_failure() { + let mut context = Context::new(); + assert!(matches!( + context.set_global_property_value(Property3, + Property3Type { + field_int: 1 + }), + Err(IxaError::IxaError(_)) + )); + } + #[test] - fn validate_property_success() { + fn validate_property_load_success() { let mut context = Context::new(); let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests/data/global_properties_valid.json"); @@ -448,7 +468,7 @@ mod test { } #[test] - fn validate_property_failure() { + fn validate_property_load_failure() { let mut context = Context::new(); let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests/data/global_properties_invalid.json"); From 23763400d87e59dbde0356346828c5a8a74a9b3a Mon Sep 17 00:00:00 2001 From: Eric Rescorla Date: Sun, 1 Dec 2024 17:01:38 -0800 Subject: [PATCH 2/2] FMT --- src/global_properties.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/global_properties.rs b/src/global_properties.rs index f0d343c..b23923e 100644 --- a/src/global_properties.rs +++ b/src/global_properties.rs @@ -443,22 +443,20 @@ mod test { #[test] fn validate_property_set_success() { let mut context = Context::new(); - context.set_global_property_value(Property3, Property3Type { - field_int: 0}).unwrap(); + context + .set_global_property_value(Property3, Property3Type { field_int: 0 }) + .unwrap(); } - + #[test] fn validate_property_set_failure() { let mut context = Context::new(); assert!(matches!( - context.set_global_property_value(Property3, - Property3Type { - field_int: 1 - }), + context.set_global_property_value(Property3, Property3Type { field_int: 1 }), Err(IxaError::IxaError(_)) )); } - + #[test] fn validate_property_load_success() { let mut context = Context::new();