Validate() public method

Validates a property or an entity.
public Validate ( System.Data.Entity.Internal.Validation.EntityValidationContext entityValidationContext, InternalMemberEntry property ) : IEnumerable
entityValidationContext System.Data.Entity.Internal.Validation.EntityValidationContext Validation context. Never null.
property InternalMemberEntry Property to validate. Null for entity validation. Not null for property validation.
return IEnumerable
        public void ValidationAttributeValidator_does_not_return_errors_if_property_value_is_valid()
        {
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary<string, object>
                    {
                        { "Name", "abc" }
                    });

            var ValidationAttributeValidator = new ValidationAttributeValidator(new StringLengthAttribute(10), null);

            var results = ValidationAttributeValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Name"));

            Assert.False(results.Any());
        }
        public void ValidationAttributeValidator_returns_validation_errors_if_property_value_is_not_valid()
        {
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary<string, object>
                    {
                        { "Name", "abcdefghijklmnopq" }
                    });

            var ValidationAttributeValidator = new ValidationAttributeValidator(new StringLengthAttribute(10), null);

            var results = ValidationAttributeValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Name"));

            Assert.Equal(1, results.Count());
            var error = results.Single();
            Assert.Equal("Name", error.PropertyName);
            Assert.Equal(string.Format(StringLengthAttribute_ValidationError, "Name", 10), error.ErrorMessage);
        }
        public void ValidationAttributeValidator_returns_errors_for_invalid_complex_property_child_property_values()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = "???",
                    }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidationAttributeValidator(new RegularExpressionAttribute("^[A-Z]{3}$"), null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport").Property("AirportCode"));

            ValidationErrorHelper.VerifyResults(
                new[]
                    {
                        new Tuple<string, string>(
                            "Departure.Airport.AirportCode",
                            string.Format(RegexAttribute_ValidationError, "Departure.Airport.AirportCode", "^[A-Z]{3}$"))
                    }, results);
        }
        public void ValidationAttributeValidator_wraps_exceptions()
        {
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary<string, object>
                    {
                        { "ID", 1 }
                    });

            var ValidationAttributeValidator = new ValidationAttributeValidator(new StringLengthAttribute(10), null);

            Assert.Equal(
                new DbUnexpectedValidationException(
                    Strings.DbUnexpectedValidationException_ValidationAttribute(
                        "ID", "System.ComponentModel.DataAnnotations.StringLengthAttribute")).Message,
                Assert.Throws<DbUnexpectedValidationException>(
                    () => ValidationAttributeValidator.Validate(
                        MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                        mockInternalEntityEntry.Object.Property("ID"))).Message);
        }
        public void ValidationAttributeValidator_returns_validation_errors_if_entity_validation_with_type_level_annotation_attributes_fails()
        {
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary<string, object>
                    {
                        { "Name", "abcdefghijklmnopq" }
                    });
            mockInternalEntityEntry.Setup(e => e.Entity).Returns(new object());

            var ValidationAttributeValidator = new ValidationAttributeValidator(
                new CustomValidationAttribute(GetType(), "FailMiserably"), null);

            var results = ValidationAttributeValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object), null);

            Assert.Equal(1, results.Count());
            var error = results.Single();
            Assert.Equal(null, error.PropertyName);
            Assert.Equal("The entity is not valid", error.ErrorMessage);
        }
        public void ValidationAttributeValidator_returns_errors_if_complex_property_type_level_validation_fails()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = "YVR",
                        CityCode = "YVR",
                        CountryCode = "ZZ"
                    }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidationAttributeValidator(new CustomValidationAttribute(typeof(AirportDetails), "ValidateCountry"), null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport"));

            ValidationErrorHelper.VerifyResults(
                new[]
                    {
                        new Tuple<string, string>("Departure.Airport", "City 'YVR' is not located in country 'ZZ'.")
                    }, results);
        }