ObjectValidation problem

Jun 5, 2014 at 4:18 AM
Edited Jun 5, 2014 at 4:19 AM
Found a problem on Data validation, Not sure is it my code problem?
the result is Suppose to be false, but it is true here.
Seems [Required] works, but [Range] not working.
[TestClass]
    public class ValidationTest {
        class Dog{
            [Required()]
            public string Name { get; set; }
            [Range(0,20)]
            public int Age { get; set; }
        }
        [TestMethod]
        public void ObjectValidationTest(){
            var dog = new Dog(){Age = -1,Name = "Jim"};
            ICollection<ValidationResult> validationResults = new List<ValidationResult>();
            var result = dog.TryValidate(validationResults);
            Assert.IsFalse(result);
        }
    }
Jun 5, 2014 at 2:54 PM
There is indeed a bug. Apparently the default behavior for Validator.TryValidate (which is what the extension method wraps) is to only validate Required properties... Why the hell you would make that the default, I don't know. Anyway, I'm pushing up a fix now and adding your above code to the unit tests (along with a couple other checks).
Jun 12, 2014 at 7:25 PM
My code has issues, it is better to put Required with other Range.