public void ComplexTypeValidator_returns_an_error_if_IValidatableObject_validation_failed()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = "???",
                    }
                }
            };

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

            var entityValidator = new ComplexTypeValidator(
                new PropertyValidator[0],
                new[] { MockHelper.CreateValidatableObjectValidator("object", "IValidatableObject is invalid") });

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

            Assert.NotNull(entityValidationResult);

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple<string, string>("object", "IValidatableObject is invalid") }, entityValidationResult);
        }
Ejemplo n.º 2
0
        protected virtual PropertyValidator BuildPropertyValidator(
            PropertyInfo clrProperty,
            EdmProperty edmProperty,
            bool buildFacetValidators)
        {
            List <IValidator>       source     = new List <IValidator>();
            IEnumerable <Attribute> attributes = this._attributeProvider.GetAttributes(clrProperty);

            source.AddRange((IEnumerable <IValidator>) this.BuildValidationAttributeValidators(attributes));
            if (edmProperty.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType)
            {
                ComplexType          edmType = (ComplexType)edmProperty.TypeUsage.EdmType;
                ComplexTypeValidator complexTypeValidator = this.BuildComplexTypeValidator(clrProperty.PropertyType, edmType);
                if (!source.Any <IValidator>() && complexTypeValidator == null)
                {
                    return((PropertyValidator)null);
                }
                return((PropertyValidator) new ComplexPropertyValidator(clrProperty.Name, (IEnumerable <IValidator>)source, complexTypeValidator));
            }
            if (buildFacetValidators)
            {
                source.AddRange(this.BuildFacetValidators(clrProperty, (EdmMember)edmProperty, attributes));
            }
            if (!source.Any <IValidator>())
            {
                return((PropertyValidator)null);
            }
            return(new PropertyValidator(clrProperty.Name, (IEnumerable <IValidator>)source));
        }
 /// <summary>
 ///     Creates an instance of <see cref="ComplexPropertyValidator" /> for a given complex property.
 /// </summary>
 /// <param name="propertyName"> The complex property name. </param>
 /// <param name="propertyValidators"> Validators used to validate the given property. </param>
 /// <param name="complexTypeValidator"> Complex type validator. </param>
 public ComplexPropertyValidator(
     string propertyName,
     IEnumerable<IValidator> propertyValidators,
     ComplexTypeValidator complexTypeValidator)
     : base(propertyName, propertyValidators)
 {
     _complexTypeValidator = complexTypeValidator;
 }
Ejemplo n.º 4
0
 // <summary>
 // Creates an instance of <see cref="ComplexPropertyValidator" /> for a given complex property.
 // </summary>
 // <param name="propertyName"> The complex property name. </param>
 // <param name="propertyValidators"> Validators used to validate the given property. </param>
 // <param name="complexTypeValidator"> Complex type validator. </param>
 public ComplexPropertyValidator(
     string propertyName,
     IEnumerable <IValidator> propertyValidators,
     ComplexTypeValidator complexTypeValidator)
     : base(propertyName, propertyValidators)
 {
     _complexTypeValidator = complexTypeValidator;
 }