public void Validator_Object_Valid()
        {
            ValTestClass vtc = new ValTestClass();

            vtc.RequiredProperty       = "xxx";
            vtc.StringLengthProperty   = "xxx";
            vtc.NullableDoubleProperty = 2.0;

            ValidationContext context = new ValidationContext(vtc, null, null);
            bool isValid = Validator.TryValidateObject(vtc, context, null);

            Assert.IsTrue(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = Validator.TryValidateObject(vtc, context, output);
            Assert.IsTrue(isValid);
            Assert.AreEqual(0, output.Count);

            // do not set the required DoubleProperty, verify object is not valid when validateAllProperties is true
            isValid = Validator.TryValidateObject(vtc, context, null, true /*validateAllProperties*/);
            Assert.IsFalse(isValid);

            // now set the DoubleProperty, verify object is now valid
            vtc.DoubleProperty = 3.0;
            isValid            = Validator.TryValidateObject(vtc, context, null, true /*validateAllProperties*/);
            Assert.IsTrue(isValid);
        }
        public void Validator_Property_Invalid_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "RequiredProperty";

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateProperty(null, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
        public void Validator_Fail_Property_Null_ValueType_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "DoubleProperty";

            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.ValidateProperty(null, context);   // DoubleProperty is a double and should fail
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "DoubleProperty", typeof(double).FullName));
        }
        // Method level validator
        public static ValidationResult IsValTestMethodValid(object vtcObject, ValidationContext context)
        {
            ValTestClass vtc    = vtcObject as ValTestClass;
            bool         result = vtc != null && vtc._failMethod == false;

            if (!result)
            {
                return(new ValidationResult("-" + context.DisplayName));
            }
            return(null);
        }
        public void Validator_Object_Invalid_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            //vtc.RequiredProperty = null causes validation error for [Required]
            //vtc.StringLengthProperty = null causes validation error for object;
            vtc.NullableDoubleProperty = null;

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateObject(vtc, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
        public void Validator_Instance_Must_Match_ValidationContext_Instance_For_Object_Validation()
        {
            ValidationContext context  = new ValidationContext(this, null, null);
            ValTestClass      instance = new ValTestClass();

            // Without specifying validate all properties
            ExceptionHelper.ExpectArgumentException(() => Validator.TryValidateObject(instance, context, null), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
            ExceptionHelper.ExpectArgumentException(() => Validator.ValidateObject(instance, context), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);

            // With specifying validate all properties
            ExceptionHelper.ExpectArgumentException(() => Validator.TryValidateObject(instance, context, null, true), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
            ExceptionHelper.ExpectArgumentException(() => Validator.ValidateObject(instance, context, true), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
        }
        public void Validator_Property_Invalid_Inherits_DisplayName()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "StringLengthProperty";

            List <ValidationResult> output = new List <ValidationResult>();
            bool isValid = Validator.TryValidateProperty("LongerThan10CharactersFails", context, output);

            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.StringLengthAttribute_ValidationError, "StringPropertyDisplayName", 10));
        }
        public void Validator_Property_Errors_Preclude_Type_Validation()
        {
            var errors  = new Collection <ValidationResult>();
            var testObj = new ValTestClass();

            // Validate the test class without [Required] properties set, this should fail.
            ValidationContext context = new ValidationContext(testObj, null, null);

            Assert.IsFalse(Validator.TryValidateObject(testObj, context, errors));

            Assert.AreEqual <int>(2, errors.Count);
            Assert.AreEqual <string>(string.Format(DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), errors[0].ErrorMessage);
            Assert.AreEqual <string>(string.Format(DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"), errors[1].ErrorMessage);
        }
        // Cross-field validation -- 2 properties must be non-null and equal
        public static ValidationResult IsValTestValid(object vtcObject, ValidationContext context)
        {
            ValTestClass vtc    = vtcObject as ValTestClass;
            bool         result = vtc != null &&
                                  vtc.RequiredProperty != null &&
                                  vtc.StringLengthProperty != null &&
                                  vtc.StringLengthProperty.Equals(vtc.RequiredProperty, StringComparison.Ordinal);

            if (!result)
            {
                return(new ValidationResult("!" + context.DisplayName));
            }
            return(null);
        }
        public void Validator_DisplayNameEmpty_UseMemberName()
        {
            ValTestClass vtc = new ValTestClass();

            vtc.RequiredProperty                   = vtc.StringLengthProperty = "required";
            vtc.NullableDoubleProperty             = vtc.DoubleProperty = 1.0d;
            vtc.StringPropertyWithEmptyDisplayName = "invalid string length";
            List <ValidationResult> results = new List <ValidationResult>();

            ValidationContext context = new ValidationContext(vtc, null, null);

            Assert.IsFalse(Validator.TryValidateObject(vtc, context, results, true));
            Assert.AreEqual <int>(1, results.Count);
            Assert.AreEqual <string>(String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.StringLengthAttribute_ValidationError, "StringPropertyWithEmptyDisplayName", 2), results[0].ErrorMessage);
        }
        public void Validator_Property_Valid()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "RequiredProperty";

            bool isValid = Validator.TryValidateProperty("xxx", context, null);

            Assert.IsTrue(isValid);

            context            = new ValidationContext(vtc, null, null);
            context.MemberName = "StringLengthProperty";
            isValid            = Validator.TryValidateProperty("yyy", context, null);
            Assert.IsTrue(isValid);
        }
        public void Validator_Fail_Property_Type_Mismatch()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "RequiredProperty";

            // IsValid entry point
            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.TryValidateProperty(2.0, context, null); // 2.0 s/b string
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "RequiredProperty", typeof(string).FullName));

            // Validate entry point
            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.ValidateProperty(2.0, context); // 2.0 s/b string
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "RequiredProperty", typeof(string).FullName));
        }
        public void Validator_Property_Invalid()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "RequiredProperty";

            bool isValid = Validator.TryValidateProperty(null, context, null);

            Assert.IsFalse(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = Validator.TryValidateProperty(null, context, output);
            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"));
        }
        public void Validator_Property_Invalid_Multiple_Reasons()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "NullableDoubleProperty";

            bool isValid = Validator.TryValidateProperty(null, context, null);

            Assert.IsFalse(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = Validator.TryValidateProperty(null, context, output);
            Assert.IsFalse(isValid);

            // The [Required] is the only expected failure -- the Range will signal success for null
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"));
        }
        public void Validator_Object_Invalid()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            //vtc.RequiredProperty = null causes validation error for [Required]
            //vtc.StringLengthProperty = null causes validation error for object;
            vtc.NullableDoubleProperty = null;  // this is [Required] and should fail

            bool isValid = Validator.TryValidateObject(vtc, context, null);

            Assert.IsFalse(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = Validator.TryValidateObject(vtc, context, output);
            Assert.IsFalse(isValid);
            Assert.AreEqual(2, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"));
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"));
        }
        public void Validator_Object_Valid() {
            ValTestClass vtc = new ValTestClass();
            vtc.RequiredProperty = "xxx";
            vtc.StringLengthProperty = "xxx";
            vtc.NullableDoubleProperty = 2.0;

            ValidationContext context = new ValidationContext(vtc, null, null);
            bool isValid = Validator.TryValidateObject(vtc, context, null);
            Assert.IsTrue(isValid);

            List<ValidationResult> output = new List<ValidationResult>();
            isValid = Validator.TryValidateObject(vtc, context, output);
            Assert.IsTrue(isValid);
            Assert.AreEqual(0, output.Count);

            // do not set the required DoubleProperty, verify object is not valid when validateAllProperties is true
            isValid = Validator.TryValidateObject(vtc, context, null, true /*validateAllProperties*/);
            Assert.IsFalse(isValid);

            // now set the DoubleProperty, verify object is now valid
            vtc.DoubleProperty = 3.0;
            isValid = Validator.TryValidateObject(vtc, context, null, true /*validateAllProperties*/);
            Assert.IsTrue(isValid);
        }
        public void Validator_Property_Valid() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "RequiredProperty";

            bool isValid = Validator.TryValidateProperty("xxx", context, null);
            Assert.IsTrue(isValid);

            context = new ValidationContext(vtc, null, null);
            context.MemberName = "StringLengthProperty";
            isValid = Validator.TryValidateProperty("yyy", context, null);
            Assert.IsTrue(isValid);
        }
        public void Validator_Property_Invalid() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "RequiredProperty";

            bool isValid = Validator.TryValidateProperty(null, context, null);
            Assert.IsFalse(isValid);

            List<ValidationResult> output = new List<ValidationResult>();
            isValid = Validator.TryValidateProperty(null, context, output);
            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"));
        }
        public void Validator_Property_Invalid_Inherits_DisplayName() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "StringLengthProperty";

            List<ValidationResult> output = new List<ValidationResult>();
            bool isValid = Validator.TryValidateProperty("LongerThan10CharactersFails", context, output);
            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.StringLengthAttribute_ValidationError, "StringPropertyDisplayName", 10));
        }
        public void Validator_Property_Invalid_Multiple_Reasons() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "NullableDoubleProperty";

            bool isValid = Validator.TryValidateProperty(null, context, null);
            Assert.IsFalse(isValid);

            List<ValidationResult> output = new List<ValidationResult>();
            isValid = Validator.TryValidateProperty(null, context, output);
            Assert.IsFalse(isValid);

            // The [Required] is the only expected failure -- the Range will signal success for null
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"));
        }
        public void Validator_DisplayNameEmpty_UseMemberName() {
            ValTestClass vtc = new ValTestClass();
            vtc.RequiredProperty = vtc.StringLengthProperty = "required";
            vtc.NullableDoubleProperty = vtc.DoubleProperty = 1.0d;
            vtc.StringPropertyWithEmptyDisplayName = "invalid string length";
            List<ValidationResult> results = new List<ValidationResult>();

            ValidationContext context = new ValidationContext(vtc, null, null);
            Assert.IsFalse(Validator.TryValidateObject(vtc, context, results, true));
            Assert.AreEqual<int>(1, results.Count);
            Assert.AreEqual<string>(String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.StringLengthAttribute_ValidationError, "StringPropertyWithEmptyDisplayName", 2), results[0].ErrorMessage);
        }
        public void Validator_Object_Invalid_Throws() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            //vtc.RequiredProperty = null causes validation error for [Required]
            //vtc.StringLengthProperty = null causes validation error for object;
            vtc.NullableDoubleProperty = null;

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateObject(vtc, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
        public void Validator_Property_Invalid_Throws() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "RequiredProperty";

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateProperty(null, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
        public void Validator_Fail_Property_Null_ValueType_Throws() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "DoubleProperty";

            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.ValidateProperty(null, context);   // DoubleProperty is a double and should fail
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "DoubleProperty", typeof(double).FullName));
        }
        public void Validator_Fail_Property_Type_Mismatch() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);
            context.MemberName = "RequiredProperty";

            // IsValid entry point
            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.TryValidateProperty(2.0, context, null); // 2.0 s/b string
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "RequiredProperty", typeof(string).FullName));

            // Validate entry point
            ExceptionHelper.ExpectArgumentException(delegate() {
                Validator.ValidateProperty(2.0, context); // 2.0 s/b string
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.Validator_Property_Value_Wrong_Type, "RequiredProperty", typeof(string).FullName));
        }
        public void Validator_Property_Errors_Preclude_Type_Validation() {
            var errors = new Collection<ValidationResult>();
            var testObj = new ValTestClass();

            // Validate the test class without [Required] properties set, this should fail.
            ValidationContext context = new ValidationContext(testObj, null, null);
            Assert.IsFalse(Validator.TryValidateObject(testObj, context, errors));

            Assert.AreEqual<int>(2, errors.Count);
            Assert.AreEqual<string>(string.Format(DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), errors[0].ErrorMessage);
            Assert.AreEqual<string>(string.Format(DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"), errors[1].ErrorMessage);
        }
        public void Validator_Instance_Must_Match_ValidationContext_Instance_For_Object_Validation() {
            ValidationContext context = new ValidationContext(this, null, null);
            ValTestClass instance = new ValTestClass();

            // Without specifying validate all properties
            ExceptionHelper.ExpectArgumentException(() => Validator.TryValidateObject(instance, context, null), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
            ExceptionHelper.ExpectArgumentException(() => Validator.ValidateObject(instance, context), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);

            // With specifying validate all properties
            ExceptionHelper.ExpectArgumentException(() => Validator.TryValidateObject(instance, context, null, true), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
            ExceptionHelper.ExpectArgumentException(() => Validator.ValidateObject(instance, context, true), DataAnnotationsResources.Validator_InstanceMustMatchValidationContextInstance);
        }
        public void Validator_Object_Invalid() {
            ValTestClass vtc = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            //vtc.RequiredProperty = null causes validation error for [Required]
            //vtc.StringLengthProperty = null causes validation error for object;
            vtc.NullableDoubleProperty = null;  // this is [Required] and should fail

            bool isValid = Validator.TryValidateObject(vtc, context, null);
            Assert.IsFalse(isValid);

            List<ValidationResult> output = new List<ValidationResult>();
            isValid = Validator.TryValidateObject(vtc, context, output);
            Assert.IsFalse(isValid);
            Assert.AreEqual(2, output.Count);
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"));
            UnitTestHelper.AssertListContains(output, String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RequiredAttribute_ValidationError, "NullableDoubleProperty"));
        }