// <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>(); }
// <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>()); }
public virtual IEnumerable <DbValidationError> Validate( EntityValidationContext entityValidationContext, InternalMemberEntry property) { ValidationContext validationContext = entityValidationContext.ExternalValidationContext; validationContext.SetDisplayName(property, this._displayAttribute); object obj = property == null ? entityValidationContext.InternalEntity.Entity : property.CurrentValue; ValidationResult validationResult; try { validationResult = this._validationAttribute.GetValidationResult(obj, validationContext); } catch (Exception ex) { throw new DbUnexpectedValidationException(Strings.DbUnexpectedValidationException_ValidationAttribute((object)validationContext.DisplayName, (object)this._validationAttribute.GetType()), ex); } if (validationResult == ValidationResult.Success) { return(Enumerable.Empty <DbValidationError>()); } return(DbHelpers.SplitValidationResults(validationContext.MemberName, (IEnumerable <ValidationResult>) new ValidationResult[1] { validationResult })); }
/// <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> /// Returns a validator to validate <paramref name="property" />. /// </summary> /// <param name="property"> Navigation property the validator is requested for. </param> /// <returns> /// Validator to validate <paramref name="property" /> . Possibly null if no validation has been specified for the requested property. /// </returns> public virtual PropertyValidator GetPropertyValidator( InternalEntityEntry owningEntity, InternalMemberEntry property) { DebugCheck.NotNull(owningEntity); DebugCheck.NotNull(property); var entityValidator = GetEntityValidator(owningEntity); return(entityValidator != null?GetValidatorForProperty(entityValidator, property) : null); }
/// <summary> /// Returns a validator to validate <paramref name="property" />. /// </summary> /// <param name="property"> Navigation property the validator is requested for. </param> /// <returns> Validator to validate <paramref name="property" /> . Possibly null if no validation has been specified for the requested property. </returns> public virtual PropertyValidator GetPropertyValidator( InternalEntityEntry owningEntity, InternalMemberEntry property) { Contract.Requires(owningEntity != null); Contract.Requires(property != null); var entityValidator = GetEntityValidator(owningEntity); return entityValidator != null ? GetValidatorForProperty(entityValidator, property) : null; }
public virtual PropertyValidator GetPropertyValidator( InternalEntityEntry owningEntity, InternalMemberEntry property) { DebugCheck.NotNull(owningEntity); DebugCheck.NotNull(property); var entityValidator = GetEntityValidator(owningEntity); return entityValidator != null ? GetValidatorForProperty(entityValidator, property) : null; }
public virtual PropertyValidator GetPropertyValidator( InternalEntityEntry owningEntity, InternalMemberEntry property) { EntityValidator entityValidator = this.GetEntityValidator(owningEntity); if (entityValidator == null) { return((PropertyValidator)null); } return(this.GetValidatorForProperty(entityValidator, property)); }
public virtual IEnumerable <DbValidationError> Validate( EntityValidationContext entityValidationContext, InternalMemberEntry property) { List <DbValidationError> dbValidationErrorList = new List <DbValidationError>(); foreach (IValidator propertyValidator in this._propertyValidators) { dbValidationErrorList.AddRange(propertyValidator.Validate(entityValidationContext, property)); } return((IEnumerable <DbValidationError>)dbValidationErrorList); }
public override IEnumerable <DbValidationError> Validate( EntityValidationContext entityValidationContext, InternalMemberEntry property) { List <DbValidationError> source = new List <DbValidationError>(); source.AddRange(base.Validate(entityValidationContext, property)); if (!source.Any <DbValidationError>() && property.CurrentValue != null && this._complexTypeValidator != null) { source.AddRange(this._complexTypeValidator.Validate(entityValidationContext, (InternalPropertyEntry)property)); } return((IEnumerable <DbValidationError>)source); }
/// <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); }
/// <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; }
public static void SetDisplayName( this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) { var displayName = displayAttribute == null ? null : displayAttribute.Name; if (property == null) { var objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType); validationContext.DisplayName = displayName ?? objectType.Name; validationContext.MemberName = null; } else { validationContext.DisplayName = displayName ?? DbHelpers.GetPropertyPath(property); validationContext.MemberName = DbHelpers.GetPropertyPath(property); } }
// <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); }
protected virtual PropertyValidator GetValidatorForProperty( EntityValidator entityValidator, InternalMemberEntry memberEntry) { InternalNestedPropertyEntry nestedPropertyEntry = memberEntry as InternalNestedPropertyEntry; if (nestedPropertyEntry == null) { return(entityValidator.GetPropertyValidator(memberEntry.Name)); } ComplexPropertyValidator validatorForProperty = this.GetValidatorForProperty(entityValidator, (InternalMemberEntry)nestedPropertyEntry.ParentPropertyEntry) as ComplexPropertyValidator; if (validatorForProperty == null || validatorForProperty.ComplexTypeValidator == null) { return((PropertyValidator)null); } return(validatorForProperty.ComplexTypeValidator.GetPropertyValidator(memberEntry.Name)); }
public static void SetDisplayName( this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) { DebugCheck.NotNull(validationContext); var displayName = displayAttribute == null ? null : displayAttribute.GetName(); if (property == null) { var objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType); validationContext.DisplayName = displayName ?? objectType.Name; validationContext.MemberName = null; } else { validationContext.DisplayName = displayName ?? DbHelpers.GetPropertyPath(property); validationContext.MemberName = DbHelpers.GetPropertyPath(property); } }
/// <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 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) { 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); }
public static void SetDisplayName( this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) { string str = displayAttribute == null ? (string)null : displayAttribute.GetName(); if (property == null) { Type objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType); validationContext.DisplayName = str ?? objectType.Name; validationContext.MemberName = (string)null; } else { validationContext.DisplayName = str ?? DbHelpers.GetPropertyPath(property); validationContext.MemberName = DbHelpers.GetPropertyPath(property); } }
// <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> /// Gets a validator for the <paramref name="memberEntry" />. /// </summary> /// <param name="entityValidator"> Entity validator. </param> /// <param name="memberEntry"> Property to get a validator for. </param> /// <returns> Validator to validate <paramref name="memberEntry" /> . Possibly null if there is no validation for the <paramref /// name="memberEntry" /> . </returns> /// <remarks> /// For complex properties this method walks up the type hierarchy to get to the entity level and then goes down /// and gets a validator for the child property that is an ancestor of the property to validate. If a validator /// returned for an ancestor is null it means that there is no validation defined beneath and the method just /// propagates (and eventually returns) null. /// </remarks> protected virtual PropertyValidator GetValidatorForProperty( EntityValidator entityValidator, InternalMemberEntry memberEntry) { var complexPropertyEntry = memberEntry as InternalNestedPropertyEntry; if (complexPropertyEntry != null) { var propertyValidator = GetValidatorForProperty(entityValidator, complexPropertyEntry.ParentPropertyEntry) as ComplexPropertyValidator; // if a validator for parent property is null there is no validation for child properties. // just propagate the null. return propertyValidator != null && propertyValidator.ComplexTypeValidator != null ? propertyValidator.ComplexTypeValidator.GetPropertyValidator(memberEntry.Name) : null; } else { return entityValidator.GetPropertyValidator(memberEntry.Name); } }
/// <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> /// Gets a validator for the <paramref name="memberEntry" />. /// </summary> /// <param name="entityValidator"> Entity validator. </param> /// <param name="memberEntry"> Property to get a validator for. </param> /// <returns> /// Validator to validate <paramref name="memberEntry" /> . Possibly null if there is no validation for the /// <paramref /// name="memberEntry" /> /// . /// </returns> /// <remarks> /// For complex properties this method walks up the type hierarchy to get to the entity level and then goes down /// and gets a validator for the child property that is an ancestor of the property to validate. If a validator /// returned for an ancestor is null it means that there is no validation defined beneath and the method just /// propagates (and eventually returns) null. /// </remarks> protected virtual PropertyValidator GetValidatorForProperty( EntityValidator entityValidator, InternalMemberEntry memberEntry) { var complexPropertyEntry = memberEntry as InternalNestedPropertyEntry; if (complexPropertyEntry != null) { var propertyValidator = GetValidatorForProperty(entityValidator, complexPropertyEntry.ParentPropertyEntry) as ComplexPropertyValidator; // if a validator for parent property is null there is no validation for child properties. // just propagate the null. return(propertyValidator != null && propertyValidator.ComplexTypeValidator != null ? propertyValidator.ComplexTypeValidator.GetPropertyValidator(memberEntry.Name) : null); } else { return(entityValidator.GetPropertyValidator(memberEntry.Name)); } }
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>())); }
internal static DbMemberEntry Create(InternalMemberEntry internalMemberEntry) { return(internalMemberEntry.CreateDbMemberEntry()); }
public PropertyValidator GetValidatorForPropertyBase(EntityValidator entityValidator, InternalMemberEntry memberEntry) { return(base.GetValidatorForProperty(entityValidator, memberEntry)); }
public PropertyValidator GetPropertyValidatorBase(InternalEntityEntry owningEntity, InternalMemberEntry property) { return(base.GetPropertyValidator(owningEntity, property)); }
public ICollection <DbValidationError> GetValidationErrors() { return(InternalMemberEntry.GetValidationErrors().ToList()); }
/// <summary> /// Creates a <see cref="DbMemberEntry" /> from information in the given <see cref="InternalMemberEntry" />. /// This method will create an instance of the appropriate subclass depending on the metadata contained /// in the InternalMemberEntry instance. /// </summary> /// <param name="internalMemberEntry"> The internal member entry. </param> /// <returns> The new entry. </returns> internal static DbMemberEntry Create(InternalMemberEntry internalMemberEntry) { DebugCheck.NotNull(internalMemberEntry); return(internalMemberEntry.CreateDbMemberEntry()); }
internal static DbMemberEntry <TEntity, TProperty> Create( InternalMemberEntry internalMemberEntry) { return(internalMemberEntry.CreateDbMemberEntry <TEntity, TProperty>()); }
public PropertyValidator GetValidatorForPropertyBase(EntityValidator entityValidator, InternalMemberEntry memberEntry) { return base.GetValidatorForProperty(entityValidator, memberEntry); }
public PropertyValidator GetPropertyValidatorBase(InternalEntityEntry owningEntity, InternalMemberEntry property) { return base.GetPropertyValidator(owningEntity, property); }
// <summary> // Creates a <see cref="DbMemberEntry{TEntity,TProperty}" /> from information in the given // <see // cref="InternalMemberEntry" /> // . // This method will create an instance of the appropriate subclass depending on the metadata contained // in the InternalMemberEntry instance. // </summary> // <param name="internalMemberEntry"> The internal member entry. </param> // <returns> The new entry. </returns> internal static DbMemberEntry <TEntity, TProperty> Create(InternalMemberEntry internalMemberEntry) { DebugCheck.NotNull(internalMemberEntry); return(internalMemberEntry.CreateDbMemberEntry <TEntity, TProperty>()); }
// <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; }