Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit a4fe4e9c authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

bugfix in validation: key and value of KeyValuePairs (e.g. Dictionary) have not been validated

parent 47fc4c50
No related branches found
No related tags found
No related merge requests found
......@@ -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++;
}
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment