コード例 #1
0
        /// <summary>
        /// Tries to validate the property.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns><c>true</c> if valid, <c>false</c> otherwise.</returns>
        private bool TryValidateProperty(AttributeTestModel model, string propertyName)
        {
            Type t   = typeof(AttributeTestModel);
            var  ctx = new ValidationContext(model);

            ctx.MemberName = propertyName;
            return(Validator.TryValidateProperty(t.GetProperty(propertyName).GetValue(model), ctx, null));
        }
コード例 #2
0
        public void RequiredWhenNotValidationIsCorrect()
        {
            var passModel1 = new AttributeTestModel
            {
                Property6  = 1.1M,
                Condition6 = "Required"
            };

            var passModel2 = new AttributeTestModel
            {
                Property6  = 0,
                Condition6 = "Optional"
            };

            var failModel = new AttributeTestModel
            {
                Property6  = 0,
                Condition6 = "asdf"
            };

            Assert.IsTrue(TryValidateProperty(passModel1, "Property6"), "Attribute validation for model 1 did not pass as expected.");
            Assert.IsTrue(TryValidateProperty(passModel2, "Property6"), "Attribute validation for model 2 did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel, "Property6"), "Attribute validation did not fail as expected.");
        }
コード例 #3
0
        public void NotAllowedWhenNotValidationIsCorrect()
        {
            var passModel1 = new AttributeTestModel
            {
                Property4  = null,
                Condition4 = "Bad"
            };

            var passModel2 = new AttributeTestModel
            {
                Property4  = "allowed",
                Condition4 = "OK"
            };

            var failModel = new AttributeTestModel
            {
                Property4  = "not allowed",
                Condition4 = "Bad"
            };

            Assert.IsTrue(TryValidateProperty(passModel1, "Property4"), "Attribute validation for model 1 did not pass as expected.");
            Assert.IsTrue(TryValidateProperty(passModel2, "Property4"), "Attribute validation for model 2 did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel, "Property4"), "Attribute validation did not fail as expected.");
        }
コード例 #4
0
        public void NotAllowedWhenValidationIsCorrect()
        {
            var passModel1 = new AttributeTestModel
            {
                Property3  = 0,
                Condition3 = true
            };

            var passModel2 = new AttributeTestModel
            {
                Property3  = 1,
                Condition3 = false
            };

            var failModel = new AttributeTestModel
            {
                Property3  = 1,
                Condition3 = true
            };

            Assert.IsTrue(TryValidateProperty(passModel1, "Property3"), "Attribute validation for model 1 did not pass as expected.");
            Assert.IsTrue(TryValidateProperty(passModel2, "Property3"), "Attribute validation for model 2 did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel, "Property3"), "Attribute validation did not fail as expected.");
        }
コード例 #5
0
        public void MustBeWhenNotValidationIsCorrect()
        {
            var passModel1 = new AttributeTestModel
            {
                Property2  = 1,
                Condition2 = false
            };

            var passModel2 = new AttributeTestModel
            {
                Property2  = 2,
                Condition2 = true
            };

            var failModel = new AttributeTestModel
            {
                Property2  = 2,
                Condition2 = false
            };

            Assert.IsTrue(TryValidateProperty(passModel1, "Property2"), "Attribute validation for model 1 did not pass as expected.");
            Assert.IsTrue(TryValidateProperty(passModel2, "Property2"), "Attribute validation for model 2 did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel, "Property2"), "Attribute validation did not fail as expected.");
        }
コード例 #6
0
        public void GreaterThanOrEqualValidationIsCorrect()
        {
            var passModel1 = new AttributeTestModel
            {
                TestStartNumber = 1,
                TestEndNumber   = 1
            };

            var passModel2 = new AttributeTestModel
            {
                TestStartNumber = 1,
                TestEndNumber   = 2
            };

            var failModel = new AttributeTestModel
            {
                TestStartNumber = 2,
                TestEndNumber   = 1
            };

            Assert.IsTrue(TryValidateProperty(passModel1, "TestEndNumber"), "Attribute validation for model 1 did not pass as expected.");
            Assert.IsTrue(TryValidateProperty(passModel2, "TestEndNumber"), "Attribute validation for model 2 did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel, "TestEndNumber"), "Attribute validation did not fail as expected.");
        }
コード例 #7
0
        public void GreaterThanValidationIsCorrect()
        {
            var passModel = new AttributeTestModel
            {
                TestStartDate = new DateTime(2016, 1, 1),
                TestEndDate   = new DateTime(2016, 2, 1)
            };

            var failModel1 = new AttributeTestModel
            {
                TestStartDate = new DateTime(2016, 1, 1),
                TestEndDate   = new DateTime(2016, 1, 1)
            };

            var failModel2 = new AttributeTestModel
            {
                TestStartDate = new DateTime(2016, 2, 1),
                TestEndDate   = new DateTime(2016, 1, 1)
            };

            Assert.IsTrue(TryValidateProperty(passModel, "TestEndDate"), "Attribute validation did not pass as expected.");
            Assert.IsFalse(TryValidateProperty(failModel1, "TestEndDate"), "Attribute validation for model 1 did not fail as expected.");
            Assert.IsFalse(TryValidateProperty(failModel2, "TestEndDate"), "Attribute validation for model 2 did not fail as expected.");
        }