Пример #1
0
        public void ComplexType_CustomValidator_MultipleMemberNames()
        {
            // ensure TDPs are registered
            DomainServiceDescription.GetDescription(typeof(ComplexTypes_TestService));

            ComplexType_Parent entity = new ComplexType_Parent
            {
                ID          = 1,
                ContactInfo =
                    new ContactInfo
                {
                    Name        = "Mathew",
                    HomeAddress = new Address {
                        AddressLine1 = "47 South Wynn Rd.", City = "Oregon", State = "OH"
                    },
                    PrimaryPhone = new Phone {
                        AreaCode = "419", Number = "693-6096"
                    }
                },
            };

            // configure multi member validation errors
            DynamicTestValidator.ForcedValidationResults.Clear();
            ValidationResult contactResult = new ValidationResult("ContactInfo", new string[] { "Name", "PrimaryPhone" });
            ValidationResult phoneResult   = new ValidationResult("Phone", new string[] { "AreaCode", "Number" });

            DynamicTestValidator.ForcedValidationResults[entity.ContactInfo] = contactResult;
            DynamicTestValidator.ForcedValidationResults[typeof(Phone)]      = phoneResult;

            ValidationContext       validationContext = ValidationUtilities.CreateValidationContext(entity, null);
            List <ValidationResult> results           = new List <ValidationResult>();
            bool isValid = ValidationUtilities.TryValidateObject(entity, validationContext, results);

            Assert.IsFalse(isValid);

            // Verify that the member names have been transformed into full paths
            ValidationResult result = results.Single(q => q.ErrorMessage == "ContactInfo-ContactInfo");

            string[] memberNames = result.MemberNames.ToArray();
            Assert.AreEqual(2, memberNames.Length);
            Assert.IsTrue(memberNames.Contains("ContactInfo.Name"));
            Assert.IsTrue(memberNames.Contains("ContactInfo.PrimaryPhone"));

            // here we expect member names to be transformed into full paths
            result      = results.Single(q => q.ErrorMessage == "Phone-TypeLevel");
            memberNames = result.MemberNames.ToArray();
            Assert.AreEqual(2, memberNames.Length);
            Assert.IsTrue(memberNames.Contains("ContactInfo.PrimaryPhone.AreaCode"));
            Assert.IsTrue(memberNames.Contains("ContactInfo.PrimaryPhone.Number"));
        }
        public void Validation_Object_Invalid_Collection_Errors()
        {
            ValTestClassWithCollection entity = new ValTestClassWithCollection();

            entity.Strings.Add("Invalid");

            ValidationContext       validationContext = new ValidationContext(entity, null, null);
            List <ValidationResult> validationResults = new List <ValidationResult>();

            bool result = ValidationUtilities.TryValidateObject(entity, validationContext, validationResults);

            Assert.IsFalse(result,
                           "Validation should fail.");
            Assert.AreEqual(1, validationResults.Count(),
                            "There should be 1 validation error");

            Assert.AreEqual("Value contains an invalid string", validationResults[0].ErrorMessage,
                            "The validation error should concern invalid string");
            Assert.AreEqual(1, validationResults[0].MemberNames.Count(),
                            "The validation error should be for a single member");
            Assert.AreEqual("Strings", validationResults[0].MemberNames.First(),
                            "The validation error should be for Strings");
        }
Пример #3
0
        public void TestEntityValidation_ComplexTypes()
        {
            // ensure TDPs are registered
            DomainServiceDescription.GetDescription(typeof(ComplexTypes_TestService));

            ComplexType_Parent entity = new ComplexType_Parent
            {
                ID          = 1,
                ContactInfo =
                    new ContactInfo
                {
                    Name        = "Mathew",
                    HomeAddress = new Address {
                        AddressLine1 = "47 South Wynn Rd.", City = "Oregon", State = "OH"
                    },
                    PrimaryPhone = new Phone {
                        AreaCode = "419", Number = "693-6096"
                    }
                },
            };

            ValidationContext       validationContext = ValidationUtilities.CreateValidationContext(entity, null);
            List <ValidationResult> results           = new List <ValidationResult>();
            bool isValid = ValidationUtilities.TryValidateObject(entity, validationContext, results);

            Assert.IsTrue(isValid && results.Count == 0);

            // set an invalid property and revalidate
            entity.ContactInfo.PrimaryPhone.AreaCode = "Invalid";
            results = new List <ValidationResult>();
            isValid = ValidationUtilities.TryValidateObject(entity, validationContext, results);
            Assert.IsTrue(!isValid && results.Count == 1);
            ValidationResult result = results.Single();

            Assert.AreEqual("The field AreaCode must be a string with a maximum length of 3.", result.ErrorMessage);
            Assert.AreEqual("ContactInfo.PrimaryPhone.AreaCode", result.MemberNames.Single());

            // create TWO validation errors
            entity.ContactInfo.PrimaryPhone.AreaCode = "Invalid";
            entity.ContactInfo.HomeAddress.State     = "Invalid";
            results = new List <ValidationResult>();
            isValid = ValidationUtilities.TryValidateObject(entity, validationContext, results);
            Assert.IsTrue(!isValid && results.Count == 2);
            Assert.AreEqual("The field State must be a string with a maximum length of 2.", results[0].ErrorMessage);
            Assert.AreEqual("ContactInfo.HomeAddress.State", results[0].MemberNames.Single());
            Assert.AreEqual("The field AreaCode must be a string with a maximum length of 3.", results[1].ErrorMessage);
            Assert.AreEqual("ContactInfo.PrimaryPhone.AreaCode", results[1].MemberNames.Single());

            // verify custom validation was called at CT type and property levels
            entity.ContactInfo.PrimaryPhone.AreaCode = "419";
            entity.ContactInfo.HomeAddress.State     = "OH";
            DynamicTestValidator.Monitor(true);
            results = new List <ValidationResult>();
            isValid = ValidationUtilities.TryValidateObject(entity, validationContext, results);
            Assert.AreEqual(3, DynamicTestValidator.ValidationCalls.Count);
            Assert.IsNotNull(DynamicTestValidator.ValidationCalls.Single(v => v.MemberName == null && v.ObjectType == typeof(ContactInfo)));
            Assert.IsNotNull(DynamicTestValidator.ValidationCalls.Single(v => v.MemberName == "ContactInfo" && v.ObjectType == typeof(ComplexType_Parent)));
            Assert.IsNotNull(DynamicTestValidator.ValidationCalls.Single(v => v.MemberName == null && v.ObjectType == typeof(Phone)));
            DynamicTestValidator.Monitor(false);

            // verify deep CT collection validation
            List <ComplexType_Recursive> children = new List <ComplexType_Recursive> {
                new ComplexType_Recursive {
                    P1 = "1", P4 = -1
                },                                                // invalid element
                new ComplexType_Recursive {
                    P1 = "2", P3 =
                        new List <ComplexType_Recursive> {
                        new ComplexType_Recursive {
                            P1 = "3", P4 = -5
                        }                                                // invalid element in nested collection
                    }
                }
            };
            ComplexType_Scenarios_Parent parent = new ComplexType_Scenarios_Parent
            {
                ID = 1,
                ComplexType_Recursive = new ComplexType_Recursive {
                    P1 = "1", P3 = children
                }
            };

            validationContext = ValidationUtilities.CreateValidationContext(parent, null);
            results           = new List <ValidationResult>();
            isValid           = ValidationUtilities.TryValidateObject(parent, validationContext, results);
            Assert.AreEqual(2, results.Count);
            result = results.Single(p => p.MemberNames.Single() == "ComplexType_Recursive.P3().P4");
            Assert.AreEqual("The field P4 must be between 0 and 5.", result.ErrorMessage);
            result = results.Single(p => p.MemberNames.Single() == "ComplexType_Recursive.P3().P3().P4");
            Assert.AreEqual("The field P4 must be between 0 and 5.", result.ErrorMessage);
        }