From a4fe4e9c0d5ab51cacb2642e169593aa445135d2 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Thu, 15 Sep 2016 16:33:09 +0200 Subject: [PATCH] bugfix in validation: key and value of KeyValuePairs (e.g. Dictionary) have not been validated --- VectoCommon/VectoCommon/Utils/Validation.cs | 28 +++++++++++++++---- .../SimulationComponentData/ValidationTest.cs | 24 ++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/VectoCommon/VectoCommon/Utils/Validation.cs b/VectoCommon/VectoCommon/Utils/Validation.cs index 1d58d04429..9044a58ac5 100644 --- a/VectoCommon/VectoCommon/Utils/Validation.cs +++ b/VectoCommon/VectoCommon/Utils/Validation.cs @@ -167,11 +167,29 @@ namespace TUGraz.VectoCommon.Utils if (enumerable != null) { var i = 0; foreach (var element in enumerable) { - var results = element.Validate(mode); - if (results.Any()) { - return new ValidationResult( - string.Format("{1}[{0}] in {1} invalid: {2}", i, validationContext.DisplayName, - string.Join("\n", results))); + if (element != null) { + var valueType = element.GetType(); + if (valueType.IsGenericType) { + var baseType = valueType.GetGenericTypeDefinition(); + if (baseType == typeof(KeyValuePair<,>)) { + var kvResults = new List<ValidationResult>(); + kvResults.AddRange(valueType.GetProperty("Key").GetValue(element).Validate(mode)); + kvResults.AddRange(valueType.GetProperty("Value").GetValue(element).Validate(mode)); + if (kvResults.Any()) { + return new ValidationResult( + string.Format("{1}[{0}] in {1} invalid: {2}", valueType.GetProperty("Key").GetValue(element), + validationContext.DisplayName, + string.Join("\n", kvResults))); + } + } + } + + var results = element.Validate(mode); + if (results.Any()) { + return new ValidationResult( + string.Format("{1}[{0}] in {1} invalid: {2}", i, validationContext.DisplayName, + string.Join("\n", results))); + } } i++; } diff --git a/VectoCore/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs b/VectoCore/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs index b6c2795dc6..baebf0c15a 100644 --- a/VectoCore/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs +++ b/VectoCore/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs @@ -333,6 +333,20 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponentData "Validation Error: " + string.Join("\n_eng_avg", results.Select(r => r.ErrorMessage))); } + [TestMethod] + public void ValidateDictionaryTest() + { + var container = new ContainerObject() { + Elements = new Dictionary<int, WrapperObject>() { + { 2, new WrapperObject() { Value = 41 } }, + { 4, new WrapperObject() { Value = -30 } } + } + }; + + var results = container.Validate(ExecutionMode.Declaration); + Assert.AreEqual(1, results.Count); + } + /// <summary> /// VECTO-249: check upshift is above downshift /// </summary> @@ -364,6 +378,16 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponentData Assert.IsTrue(results.Any()); } + public class ContainerObject + { + [Required, ValidateObject] public Dictionary<int, WrapperObject> Elements; + } + + public class WrapperObject + { + [Required, Range(0, 100)] public int Value = 0; + } + public class DeepDataObject { [Required, Range(41, 42)] protected int public_field = 5; -- GitLab