Skip to content

Commit 6bcebc4

Browse files
author
Mike Reust
committed
Fix short-circuit issue
1 parent a12beb0 commit 6bcebc4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

DataAnnotationsValidator/DataAnnotationsValidator.Tests/DataAnnotationsValidatorTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,22 @@ public void TryValidateObject_calls_grandchild_IValidatableObject_method()
105105
Assert.AreEqual(1, validationResults.Count);
106106
Assert.AreEqual(1, validationResults.ToList().Count(x => x.ErrorMessage == "GrandChild PropertyA and PropertyB cannot add up to more than 10"));
107107
}
108+
109+
[Test]
110+
public void TryValidateObject_includes_errors_from_all_objects()
111+
{
112+
var parent = new Parent { PropertyA = 5, PropertyB = 6 };
113+
parent.Child = new Child { PropertyA = 5, PropertyB = 6 };
114+
parent.Child.GrandChildren = new[] { new GrandChild { PropertyA = 5, PropertyB = 6 } };
115+
var validationResults = new List<ValidationResult>();
116+
117+
var result = DataAnnotationsValidator.TryValidateObjectRecursive(parent, validationResults);
118+
119+
Assert.IsFalse(result);
120+
Assert.AreEqual(3, validationResults.Count);
121+
Assert.AreEqual(1, validationResults.ToList().Count(x => x.ErrorMessage == "Parent PropertyA and PropertyB cannot add up to more than 10"));
122+
Assert.AreEqual(1, validationResults.ToList().Count(x => x.ErrorMessage == "Child PropertyA and PropertyB cannot add up to more than 10"));
123+
Assert.AreEqual(1, validationResults.ToList().Count(x => x.ErrorMessage == "GrandChild PropertyA and PropertyB cannot add up to more than 10"));
124+
}
108125
}
109126
}

DataAnnotationsValidator/DataAnnotationsValidator/DataAnnotationsValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public static bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> r
2929
{
3030
foreach (var enumObj in asEnumerable)
3131
{
32-
result = result && TryValidateObjectRecursive(enumObj, results);
32+
result = TryValidateObjectRecursive(enumObj, results) && result;
3333
}
3434
}
3535
else
3636
{
37-
result = result && TryValidateObjectRecursive(value, results);
37+
result = TryValidateObjectRecursive(value, results) && result;
3838
}
3939
}
4040

0 commit comments

Comments
 (0)