// <summary>
        // Validates a property or an entity.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Null for entity validation. Not null for property validation. </param>
        // <returns>
        // Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors, never null.
        // </returns>
        public virtual IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);

            var validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, _displayAttribute);

            var objectToValidate = property == null
                                       ? entityValidationContext.InternalEntity.Entity
                                       : property.CurrentValue;

            ValidationResult validationResult = null;

            try
            {
                validationResult = _validationAttribute.GetValidationResult(objectToValidate, validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(
                    Strings.DbUnexpectedValidationException_ValidationAttribute(
                        validationContext.DisplayName, _validationAttribute.GetType()),
                    ex);
            }

            return validationResult != ValidationResult.Success
                       ? DbHelpers.SplitValidationResults(validationContext.MemberName, new[] { validationResult })
                       : Enumerable.Empty<DbValidationError>();
        }
Пример #2
0
        /// <summary>
        ///     Contract for IValidator.Validate method.
        /// </summary>
        /// <param name = "entityValidationContext">Validation context.</param>
        /// <param name = "property">Property.</param>
        /// <returns>Nothing - always throws.</returns>
        IEnumerable<DbValidationError> IValidator.Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            Contract.Requires(entityValidationContext != null);
            Contract.Ensures(Contract.Result<IEnumerable<DbValidationError>>() != null);

            throw new NotImplementedException();
        }
        /// <summary>
        ///     Validates an entity.
        /// </summary>
        /// <param name = "entityValidationContext">Entity validation context. Must not be null.</param>
        /// <returns><see cref = "DbEntityValidationResult" /> instance. Never null.</returns>
        public DbEntityValidationResult Validate(EntityValidationContext entityValidationContext)
        {
            Contract.Requires(entityValidationContext != null);
            Contract.Assert(entityValidationContext.InternalEntity != null);

            var validationErrors = Validate(entityValidationContext, null);

            return new DbEntityValidationResult(entityValidationContext.InternalEntity, validationErrors);
        }
        /// <summary>
        ///     Validates an entity.
        /// </summary>
        /// <param name="entityValidationContext"> Entity validation context. Must not be null. </param>
        /// <returns>
        ///     <see cref="DbEntityValidationResult" /> instance. Never null.
        /// </returns>
        public DbEntityValidationResult Validate(EntityValidationContext entityValidationContext)
        {
            DebugCheck.NotNull(entityValidationContext);
            Debug.Assert(entityValidationContext.InternalEntity != null);

            var validationErrors = Validate(entityValidationContext, null);

            return new DbEntityValidationResult(entityValidationContext.InternalEntity, validationErrors);
        }
        /// <summary>
        ///     Validates type properties. Any validation errors will be added to <paramref name = "validationErrors" />
        ///     collection.
        /// </summary>
        /// <param name = "entityValidationContext">
        ///     Validation context. Must not be null.
        /// </param>
        /// <param name = "validationErrors">
        ///     Collection of validation errors. Any validation errors will be added to it.
        /// </param>
        /// <param name = "parentProperty">The entry for the complex property. Null if validating an entity.</param>
        /// <remarks>
        ///     Note that <paramref name = "validationErrors" /> will be modified by this method. Errors should be only added,
        ///     never removed or changed. Taking a collection as a modifiable parameter saves a couple of memory allocations
        ///     and a merge of validation error lists per entity.
        /// </remarks>
        protected override void ValidateProperties(
            EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty,
            List<DbValidationError> validationErrors)
        {
            var entityEntry = entityValidationContext.InternalEntity;

            foreach (var validator in PropertyValidators)
            {
                validationErrors.AddRange(
                    validator.Validate(entityValidationContext, entityEntry.Member(validator.PropertyName)));
            }
        }
        /// <summary>
        ///     Validates type properties. Any validation errors will be added to <paramref name = "validationErrors" />
        ///     collection.
        /// </summary>
        /// <param name = "entityValidationContext">
        ///     Validation context. Must not be null.
        /// </param>
        /// <param name = "validationErrors">
        ///     Collection of validation errors. Any validation errors will be added to it.
        /// </param>
        /// <param name = "parentProperty">The entry for the complex property. Null if validating an entity.</param>
        /// <remarks>
        ///     Note that <paramref name = "validationErrors" /> will be modified by this method. Errors should be only added,
        ///     never removed or changed. Taking a collection as a modifiable parameter saves a couple of memory allocations
        ///     and a merge of validation error lists per entity.
        /// </remarks>
        protected override void ValidateProperties(
            EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty,
            List<DbValidationError> validationErrors)
        {
            Contract.Assert(parentProperty.EntryMetadata.IsComplex, "A complex type expected.");
            Contract.Assert(parentProperty.CurrentValue != null);

            foreach (var validator in PropertyValidators)
            {
                var complexProperty = parentProperty.Property(validator.PropertyName);
                validationErrors.AddRange(validator.Validate(entityValidationContext, complexProperty));
            }
        }
        /// <summary>
        ///     Validates a property.
        /// </summary>
        /// <param name = "entityValidationContext">Validation context. Never null.</param>
        /// <param name = "property">Property to validate. Never null.</param>
        /// <returns>Validation errors as <see cref = "IEnumerable{DbValidationError}" />. Empty if no errors. Never null.
        /// </returns>
        public virtual IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            //Contract.Requires(entityValidationContext != null);
            //Contract.Requires(property != null);

            var validationErrors = new List<DbValidationError>();

            foreach (var validator in _propertyValidators)
            {
                validationErrors.AddRange(validator.Validate(entityValidationContext, property));
            }

            return validationErrors;
        }
        /// <summary>
        ///     Validates a property.
        /// </summary>
        /// <param name="entityValidationContext"> Validation context. Never null. </param>
        /// <param name="property"> Property to validate. Never null. </param>
        /// <returns>
        ///     Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null.
        /// </returns>
        public virtual IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);
            DebugCheck.NotNull(property);

            var validationErrors = new List<DbValidationError>();

            foreach (var validator in _propertyValidators)
            {
                validationErrors.AddRange(validator.Validate(entityValidationContext, property));
            }

            return validationErrors;
        }
Пример #9
0
        /// <summary>
        ///     Validates an instance.
        /// </summary>
        /// <param name = "entityValidationContext">Entity validation context. Must not be null.</param>
        /// <param name = "property">The entry for the complex property. Null if validating an entity.</param>
        /// <returns><see cref = "DbEntityValidationResult" /> instance. Never null.</returns>
        /// <remarks>
        ///     Protected so it doesn't appear on EntityValidator.
        /// </remarks>
        protected IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalPropertyEntry property)
        {
            var validationErrors = new List<DbValidationError>();

            ValidateProperties(entityValidationContext, property, validationErrors);

            // only run type level validation if all properties were validated successfully
            if (!validationErrors.Any())
            {
                foreach (var typeLevelValidator in _typeLevelValidators)
                {
                    validationErrors.AddRange(typeLevelValidator.Validate(entityValidationContext, property));
                }
            }

            return validationErrors;
        }
        /// <summary>
        ///     Validates a complex property.
        /// </summary>
        /// <param name="entityValidationContext"> Validation context. Never null. </param>
        /// <param name="property"> Property to validate. Never null. </param>
        /// <returns> Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null. </returns>
        public override IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            Contract.Assert(property is InternalPropertyEntry);

            var validationErrors = new List<DbValidationError>();
            validationErrors.AddRange(base.Validate(entityValidationContext, property));

            // don't drill into complex types if there were errors or the complex property has not been initialized at all
            if (!validationErrors.Any() && property.CurrentValue != null
                &&
                _complexTypeValidator != null)
            {
                validationErrors.AddRange(
                    _complexTypeValidator.Validate(entityValidationContext, (InternalPropertyEntry)property));
            }

            return validationErrors;
        }
        /// <summary>
        ///     Validates an entity or a complex type implementing IValidatableObject interface.
        ///     This method is virtual to allow mocking.
        /// </summary>
        /// <param name="entityValidationContext"> Validation context. Never null. </param>
        /// <param name="property"> Property to validate. Null if this is the entity that will be validated. Never null if this is the complex type that will be validated. </param>
        /// <returns>
        ///     Validation error as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null.
        /// </returns>
        /// <remarks>
        ///     Note that <paramref name="property" /> is used to figure out what needs to be validated. If it not null the complex
        ///     type will be validated otherwise the entity will be validated.
        ///     Also if this is an IValidatableObject complex type but the instance (.CurrentValue) is null we won't validate
        ///     anything and will not return any errors. The reason for this is that Validation is supposed to validate using
        ///     information the user provided and not some additional implicit rules. (ObjectContext will throw for operations
        ///     that involve null complex properties).
        /// </remarks>
        public virtual IEnumerable<DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);

            Debug.Assert(
                (property == null && entityValidationContext.InternalEntity.Entity is IValidatableObject) ||
                (property != null && (property.CurrentValue == null || property.CurrentValue is IValidatableObject)),
                "Neither entity nor complex type implements IValidatableObject.");

            if (property != null
                && property.CurrentValue == null)
            {
                return Enumerable.Empty<DbValidationError>();
            }

            var validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, _displayAttribute);

            var validatableObject = (IValidatableObject)(property == null
                                                             ? entityValidationContext.InternalEntity.Entity
                                                             : property.CurrentValue);

            IEnumerable<ValidationResult> validationResults = null;
            try
            {
                validationResults = validatableObject.Validate(validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(
                    Strings.DbUnexpectedValidationException_IValidatableObject(
                        validationContext.DisplayName, ObjectContextTypeCache.GetObjectType(validatableObject.GetType())),
                    ex);
            }

            return DbHelpers.SplitValidationResults(
                validationContext.MemberName,
                validationResults ?? Enumerable.Empty<ValidationResult>());
        }
        // <summary>
        // Validates a complex property.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Never null. </param>
        // <returns>
        // Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null.
        // </returns>
        public override IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            Debug.Assert(property is InternalPropertyEntry);

            var validationErrors = new List <DbValidationError>();

            validationErrors.AddRange(base.Validate(entityValidationContext, property));

            // don't drill into complex types if there were errors or the complex property has not been initialized at all
            if (!validationErrors.Any() &&
                property.CurrentValue != null
                &&
                _complexTypeValidator != null)
            {
                validationErrors.AddRange(
                    _complexTypeValidator.Validate(entityValidationContext, (InternalPropertyEntry)property));
            }

            return(validationErrors);
        }
Пример #13
0
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext,
            InternalMemberEntry property)
        {
            if (property != null && property.CurrentValue == null)
            {
                return(Enumerable.Empty <DbValidationError>());
            }
            ValidationContext validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, this._displayAttribute);
            IValidatableObject             validatableObject = property == null ? (IValidatableObject)entityValidationContext.InternalEntity.Entity : (IValidatableObject)property.CurrentValue;
            IEnumerable <ValidationResult> validationResults;

            try
            {
                validationResults = validatableObject.Validate(validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(Strings.DbUnexpectedValidationException_IValidatableObject((object)validationContext.DisplayName, (object)ObjectContextTypeCache.GetObjectType(validatableObject.GetType())), ex);
            }
            return(DbHelpers.SplitValidationResults(validationContext.MemberName, validationResults ?? Enumerable.Empty <ValidationResult>()));
        }
        // <summary>
        // Validates a property or an entity.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Null for entity validation. Not null for property validation. </param>
        // <returns>
        // Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors, never null.
        // </returns>
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);

            if (!AttributeApplicable(entityValidationContext, property))
            {
                return(Enumerable.Empty <DbValidationError>());
            }

            var validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, _displayAttribute);

            var objectToValidate = property == null
                                       ? entityValidationContext.InternalEntity.Entity
                                       : property.CurrentValue;

            ValidationResult validationResult = null;

            try
            {
                validationResult = _validationAttribute.GetValidationResult(objectToValidate, validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(
                          Strings.DbUnexpectedValidationException_ValidationAttribute(
                              validationContext.DisplayName, _validationAttribute.GetType()),
                          ex);
            }

            return(validationResult != ValidationResult.Success
                       ? DbHelpers.SplitValidationResults(validationContext.MemberName, new[] { validationResult })
                       : Enumerable.Empty <DbValidationError>());
        }
Пример #15
0
 protected override void ValidateProperties(
     EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty,
     List<DbValidationError> validationErrors)
 {
     //Contract.Requires(entityValidationContext != null);
     //Contract.Requires(validationErrors != null);
 }
Пример #16
0
 // <summary>
 // Validates type properties. Any validation errors will be added to <paramref name="validationErrors" />
 // collection.
 // </summary>
 // <param name="entityValidationContext"> Validation context. Must not be null. </param>
 // <param name="parentProperty"> The entry for the complex property. Null if validating an entity. </param>
 // <param name="validationErrors"> Collection of validation errors. Any validation errors will be added to it. </param>
 // <remarks>
 // Note that <paramref name="validationErrors" /> will be modified by this method. Errors should be only added,
 // never removed or changed. Taking a collection as a modifiable parameter saves a couple of memory allocations
 // and a merge of validation error lists per entity.
 // </remarks>
 protected abstract void ValidateProperties(
     EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty,
     List <DbValidationError> validationErrors);
Пример #17
0
 /// <summary>
 ///     Validates an instance.
 /// </summary>
 /// <param name="entityValidationContext"> Entity validation context. Must not be null. </param>
 /// <param name="property"> The entry for the complex property. Null if validating an entity. </param>
 /// <returns>
 ///     <see cref="DbEntityValidationResult" /> instance. Never null.
 /// </returns>
 public new IEnumerable <DbValidationError> Validate(
     EntityValidationContext entityValidationContext, InternalPropertyEntry property)
 {
     return(base.Validate(entityValidationContext, property));
 }
 /// <summary>
 ///     Validates an instance.
 /// </summary>
 /// <param name = "entityValidationContext">Entity validation context. Must not be null.</param>
 /// <param name = "property">The entry for the complex property. Null if validating an entity.</param>
 /// <returns><see cref = "DbEntityValidationResult" /> instance. Never null.</returns>
 public new IEnumerable<DbValidationError> Validate(
     EntityValidationContext entityValidationContext, InternalPropertyEntry property)
 {
     return base.Validate(entityValidationContext, property);
 }
Пример #19
0
 /// <summary>
 ///     Validates type properties. Any validation errors will be added to <paramref name = "validationErrors" />
 ///     collection.
 /// </summary>
 /// <param name = "entityValidationContext">
 ///     Validation context. Must not be null.
 /// </param>
 /// <param name = "validationErrors">
 ///     Collection of validation errors. Any validation errors will be added to it.
 /// </param>
 /// <param name = "parentProperty">The entry for the complex property. Null if validating an entity.</param>
 /// <remarks>
 ///     Note that <paramref name = "validationErrors" /> will be modified by this method. Errors should be only added,
 ///     never removed or changed. Taking a collection as a modifiable parameter saves a couple of memory allocations
 ///     and a merge of validation error lists per entity.
 /// </remarks>
 protected abstract void ValidateProperties(
     EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty,
     List<DbValidationError> validationErrors);
        // <summary>
        // Determines if the attribute should be enforced given the context of the validation request.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Null for entity validation. Not null for property validation. </param>
        // <returns> True if the attribute should be enforced; otherwise false. </returns>
        protected virtual bool AttributeApplicable(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            // Do not apply RequiredAttrbiute to existing entities when the property is
            // a navigation property and it has not been loaded.
            var internalNavigationProperty = property as InternalNavigationEntry;

            if (_validationAttribute is RequiredAttribute &&
                property != null && property.InternalEntityEntry != null &&
                property.InternalEntityEntry.State != EntityState.Added && property.InternalEntityEntry.State != EntityState.Detached &&
                internalNavigationProperty != null && !internalNavigationProperty.IsLoaded)
            {
                return false;
            }

            return true;
        }