public void SetsValuesCorrectlyUsingFormattedMessage() { var validationResult = new FieldValidationResult("myProperty", ValidationResultType.Error, "my message with {0}", "format"); Assert.AreEqual("myProperty", validationResult.PropertyName); Assert.AreEqual(ValidationResultType.Error, validationResult.ValidationResultType); Assert.AreEqual("my message with format", validationResult.Message); }
public void SetsValuesCorrectlyUsingNormalMessage() { var validationResult = new FieldValidationResult("myProperty", ValidationResultType.Error, "my message"); Assert.AreEqual("myProperty", validationResult.PropertyName); Assert.AreEqual(ValidationResultType.Error, validationResult.ValidationResultType); Assert.AreEqual("my message", validationResult.Message); }
public void SetsValuesCorrectlyUsingEmptyMessage() { var validationResult = new FieldValidationResult("myProperty", ValidationResultType.Error, string.Empty); Assert.AreEqual("myProperty", validationResult.PropertyName); Assert.AreEqual(ValidationResultType.Error, validationResult.ValidationResultType); Assert.AreEqual(string.Empty, validationResult.Message); }
/// <summary> /// Validates the current object for field and business rule errors. /// </summary> /// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param> /// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param> /// <remarks> /// To check whether this object contains any errors, use the <see cref="HasErrors"/> property. /// </remarks> internal void Validate(bool force, bool validateDataAnnotations) { if (SuspendValidation) { return; } if (IsValidating) { return; } IsValidating = true; var validationContext = (ValidationContext)ValidationContext; var changes = new List <ValidationContextChange>(); bool hasErrors = HasErrors; bool hasWarnings = HasWarnings; var validator = Validator; if (validator != null) { validator.BeforeValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations()); } OnValidating(); CatchUpWithSuspendedAnnotationsValidation(); if (force && validateDataAnnotations) { // In forced mode, validate all registered properties for annotations foreach (var propertyData in PropertyDataManager.GetProperties(GetType())) { var propertyValue = GetValue(propertyData.Value); ValidatePropertyUsingAnnotations(propertyData.Key, propertyValue); } } if (!IsValidated || force) { lock (_validationLock) { var fieldValidationResults = new List <IFieldValidationResult>(); var businessRuleValidationResults = new List <IBusinessRuleValidationResult>(); #region Fields if (validator != null) { validator.BeforeValidateFields(this, validationContext.GetFieldValidations()); } OnValidatingFields(); if (validator != null) { validator.ValidateFields(this, fieldValidationResults); } #if !WINDOWS_PHONE && !NETFX_CORE && !NET35 // Support annotation validation fieldValidationResults.AddRange(from fieldAnnotationValidation in _dataAnnotationValidationResults where !string.IsNullOrEmpty(fieldAnnotationValidation.Value) select(IFieldValidationResult) FieldValidationResult.CreateError(fieldAnnotationValidation.Key, fieldAnnotationValidation.Value)); #endif ValidateFields(fieldValidationResults); // In-between validations, it might be possible that users used the SetFieldValidationResult if (_internalValidationContext != null) { fieldValidationResults.AddRange(_internalValidationContext.GetFieldValidations()); } OnValidatedFields(); if (validator != null) { validator.AfterValidateFields(this, fieldValidationResults); } #endregion #region Business rules if (validator != null) { validator.BeforeValidateBusinessRules(this, validationContext.GetBusinessRuleValidations()); } OnValidatingBusinessRules(); if (validator != null) { validator.ValidateBusinessRules(this, businessRuleValidationResults); } ValidateBusinessRules(businessRuleValidationResults); // In-between validations, it might be possible that users used the SetBusinessRuleValidationResult if (_internalValidationContext != null) { businessRuleValidationResults.AddRange(_internalValidationContext.GetBusinessRuleValidations()); } OnValidatedBusinessRules(); if (validator != null) { validator.AfterValidateBusinessRules(this, businessRuleValidationResults); } #endregion if (validator != null) { validator.Validate(this, validationContext); } IsValidated = true; // Clear internal validation _internalValidationContext = new ValidationContext(); changes = validationContext.SynchronizeWithContext(new ValidationContext(fieldValidationResults, businessRuleValidationResults)); } } OnValidated(); if (validator != null) { validator.AfterValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations()); } #region Notify changes bool hasNotifiedBusinessWarningsChanged = false; bool hasNotifiedBusinessErrorsChanged = false; foreach (var change in changes) { var changeAsFieldValidationResult = change.ValidationResult as IFieldValidationResult; var changeAsBusinessRuleValidationResult = change.ValidationResult as IBusinessRuleValidationResult; if (changeAsFieldValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: NotifyWarningsChanged(changeAsFieldValidationResult.PropertyName, false); break; case ValidationResultType.Error: NotifyErrorsChanged(changeAsFieldValidationResult.PropertyName, false); break; default: throw new ArgumentOutOfRangeException(); } } else if (changeAsBusinessRuleValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: if (!hasNotifiedBusinessWarningsChanged) { hasNotifiedBusinessWarningsChanged = true; NotifyWarningsChanged(string.Empty, false); } break; case ValidationResultType.Error: if (!hasNotifiedBusinessErrorsChanged) { hasNotifiedBusinessErrorsChanged = true; NotifyErrorsChanged(string.Empty, false); } break; default: throw new ArgumentOutOfRangeException(); } } } if (HasWarnings != hasWarnings) { RaisePropertyChanged("HasWarnings"); } if (HasErrors != hasErrors) { RaisePropertyChanged("HasErrors"); } #endregion IsValidating = false; }
/// <summary> /// Validates the current object for field and business rule errors. /// </summary> /// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param> /// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param> /// <remarks> /// To check whether this object contains any errors, use the ValidationContext property. /// </remarks> internal void Validate(bool force, bool validateDataAnnotations) { if (IsValidating) { return; } if (IsValidationSuspended) { return; } IsValidating = true; var existingValidationContext = _validationContext; if (existingValidationContext == null) { existingValidationContext = new ValidationContext(); } var hasErrors = existingValidationContext.HasErrors; var hasWarnings = existingValidationContext.HasWarnings; var validationContext = new ValidationContext(); var changes = new List <ValidationContextChange>(); var validator = GetValidator(); if (validator != null) { validator.BeforeValidation(this, existingValidationContext.GetFieldValidations(), existingValidationContext.GetBusinessRuleValidations()); } OnValidating(validationContext); if (force && validateDataAnnotations) { var type = GetType(); var ignoredOrFailedPropertyValidations = PropertiesNotCausingValidation[type]; // In forced mode, validate all registered properties for annotations var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(type); foreach (var propertyData in catelTypeInfo.GetCatelProperties()) { if (propertyData.Value.IsModelBaseProperty) { continue; } var propertyInfo = propertyData.Value.GetPropertyInfo(type); if (propertyInfo == null || !propertyInfo.HasPublicGetter) { // Note: non-public getter, do not validate ignoredOrFailedPropertyValidations.Add(propertyData.Key); continue; } ValidatePropertyUsingAnnotations(propertyData.Key); } #if !NETFX_CORE && !PCL // Validate non-catel properties as well for attribute validation foreach (var propertyInfo in catelTypeInfo.GetNonCatelProperties()) { if (_firstAnnotationValidation) { if (propertyInfo.Value.IsDecoratedWithAttribute <ExcludeFromValidationAttribute>()) { ignoredOrFailedPropertyValidations.Add(propertyInfo.Key); } } if (!propertyInfo.Value.HasPublicGetter) { // Note: non-public getter, do not validate ignoredOrFailedPropertyValidations.Add(propertyInfo.Key); continue; } // TODO: Should we check for annotations attributes? if (ignoredOrFailedPropertyValidations.Contains(propertyInfo.Key)) { continue; } try { ValidatePropertyUsingAnnotations(propertyInfo.Key); } catch (Exception ex) { Log.Warning(ex, "Failed to validate property '{0}.{1}', adding it to the ignore list", type.Name, propertyInfo.Key); ignoredOrFailedPropertyValidations.Add(propertyInfo.Key); } } _firstAnnotationValidation = false; #endif } if (!_isValidated || force) { lock (_validationContext) { var fieldValidationResults = new List <IFieldValidationResult>(); var businessRuleValidationResults = new List <IBusinessRuleValidationResult>(); #region Fields if (validator != null) { validator.BeforeValidateFields(this, validationContext.GetFieldValidations()); } OnValidatingFields(validationContext); if (validator != null) { validator.ValidateFields(this, fieldValidationResults); } #if !NETFX_CORE && !PCL // Support annotation validation fieldValidationResults.AddRange(from fieldAnnotationValidation in _dataAnnotationValidationResults where !string.IsNullOrEmpty(fieldAnnotationValidation.Value) select(IFieldValidationResult) FieldValidationResult.CreateError(fieldAnnotationValidation.Key, fieldAnnotationValidation.Value)); #endif ValidateFields(fieldValidationResults); OnValidatedFields(validationContext); if (validator != null) { validator.AfterValidateFields(this, fieldValidationResults); } // As the last step, sync the field validation results with the context foreach (var fieldValidationResult in fieldValidationResults) { validationContext.AddFieldValidationResult(fieldValidationResult); } #endregion #region Business rules if (validator != null) { validator.BeforeValidateBusinessRules(this, validationContext.GetBusinessRuleValidations()); } OnValidatingBusinessRules(validationContext); if (validator != null) { validator.ValidateBusinessRules(this, businessRuleValidationResults); } ValidateBusinessRules(businessRuleValidationResults); OnValidatedBusinessRules(validationContext); if (validator != null) { validator.AfterValidateBusinessRules(this, businessRuleValidationResults); } // As the last step, sync the field validation results with the context foreach (var businessRuleValidationResult in businessRuleValidationResults) { validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult); } #endregion if (validator != null) { validator.Validate(this, validationContext); } _isValidated = true; // Manual sync to get the changes changes = existingValidationContext.SynchronizeWithContext(validationContext); } } OnValidated(validationContext); if (validator != null) { validator.AfterValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations()); } #region Notify changes var hasNotifiedBusinessWarningsChanged = false; var hasNotifiedBusinessErrorsChanged = false; foreach (var change in changes) { var changeAsFieldValidationResult = change.ValidationResult as IFieldValidationResult; var changeAsBusinessRuleValidationResult = change.ValidationResult as IBusinessRuleValidationResult; if (changeAsFieldValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: NotifyWarningsChanged(changeAsFieldValidationResult.PropertyName, false); break; case ValidationResultType.Error: NotifyErrorsChanged(changeAsFieldValidationResult.PropertyName, false); break; default: throw new ArgumentOutOfRangeException(); } } else if (changeAsBusinessRuleValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: if (!hasNotifiedBusinessWarningsChanged) { hasNotifiedBusinessWarningsChanged = true; NotifyWarningsChanged(string.Empty, false); } break; case ValidationResultType.Error: if (!hasNotifiedBusinessErrorsChanged) { hasNotifiedBusinessErrorsChanged = true; NotifyErrorsChanged(string.Empty, false); } break; default: throw new ArgumentOutOfRangeException(); } } } if (_validationContext.HasWarnings != hasWarnings) { RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(HasWarnings)), false, false); RaisePropertyChanged(this, new PropertyChangedEventArgs(HasWarningsMessageProperty), false, false); } if (_validationContext.HasErrors != hasErrors) { RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(HasErrors)), false, false); RaisePropertyChanged(this, new PropertyChangedEventArgs(HasErrorsMessageProperty), false, false); } #endregion IsValidating = false; }
/// <summary> /// Validates the current object for field and business rule errors. /// </summary> /// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param> /// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param> /// <remarks> /// To check whether this object contains any errors, use the <see cref="HasErrors"/> property. /// </remarks> internal void Validate(bool force, bool validateDataAnnotations) { if (SuspendValidation) { return; } if (IsValidating) { return; } IsValidating = true; var validationContext = (ValidationContext)ValidationContext; var changes = new List <ValidationContextChange>(); bool hasErrors = HasErrors; bool hasWarnings = HasWarnings; var validator = Validator; if (validator != null) { validator.BeforeValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations()); } OnValidating(); CatchUpWithSuspendedAnnotationsValidation(); if (force && validateDataAnnotations) { var type = GetType(); // In forced mode, validate all registered properties for annotations var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(type); foreach (var propertyData in catelTypeInfo.GetCatelProperties()) { var propertyValue = GetValue(propertyData.Value); ValidatePropertyUsingAnnotations(propertyData.Key, propertyValue); } #if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35 // Validate non-catel properties as well for attribute validation foreach (var propertyInfo in catelTypeInfo.GetNonCatelProperties()) { var ignoredOrFailedPropertyValidations = _propertyValuesIgnoredOrFailedForValidation[type]; if (_firstAnnotationValidation) { if (AttributeHelper.IsDecoratedWithAttribute <ExcludeFromValidationAttribute>(propertyInfo.Value)) { ignoredOrFailedPropertyValidations.Add(propertyInfo.Key); } } // TODO: Should we check for annotations attributes? if (ignoredOrFailedPropertyValidations.Contains(propertyInfo.Key)) { continue; } try { var propertyValue = propertyInfo.Value.GetValue(this, null); ValidatePropertyUsingAnnotations(propertyInfo.Key, propertyValue); } catch (Exception ex) { Log.Warning(ex, "Failed to validate property '{0}.{1}', adding it to the ignore list", type.Name, propertyInfo.Key); ignoredOrFailedPropertyValidations.Add(propertyInfo.Key); } } _firstAnnotationValidation = false; #endif } if (!IsValidated || force) { lock (_validationLock) { var fieldValidationResults = new List <IFieldValidationResult>(); var businessRuleValidationResults = new List <IBusinessRuleValidationResult>(); #region Fields if (validator != null) { validator.BeforeValidateFields(this, validationContext.GetFieldValidations()); } OnValidatingFields(); if (validator != null) { validator.ValidateFields(this, fieldValidationResults); } #if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35 // Support annotation validation fieldValidationResults.AddRange(from fieldAnnotationValidation in _dataAnnotationValidationResults where !string.IsNullOrEmpty(fieldAnnotationValidation.Value) select(IFieldValidationResult) FieldValidationResult.CreateError(fieldAnnotationValidation.Key, fieldAnnotationValidation.Value)); #endif ValidateFields(fieldValidationResults); // In-between validations, it might be possible that users used the SetFieldValidationResult if (_internalValidationContext != null) { fieldValidationResults.AddRange(_internalValidationContext.GetFieldValidations()); } OnValidatedFields(); if (validator != null) { validator.AfterValidateFields(this, fieldValidationResults); } #endregion #region Business rules if (validator != null) { validator.BeforeValidateBusinessRules(this, validationContext.GetBusinessRuleValidations()); } OnValidatingBusinessRules(); if (validator != null) { validator.ValidateBusinessRules(this, businessRuleValidationResults); } ValidateBusinessRules(businessRuleValidationResults); // In-between validations, it might be possible that users used the SetBusinessRuleValidationResult if (_internalValidationContext != null) { businessRuleValidationResults.AddRange(_internalValidationContext.GetBusinessRuleValidations()); } OnValidatedBusinessRules(); if (validator != null) { validator.AfterValidateBusinessRules(this, businessRuleValidationResults); } #endregion if (validator != null) { validator.Validate(this, validationContext); } IsValidated = true; // Clear internal validation _internalValidationContext = new ValidationContext(); changes = validationContext.SynchronizeWithContext(new ValidationContext(fieldValidationResults, businessRuleValidationResults)); } } OnValidated(); if (validator != null) { validator.AfterValidation(this, validationContext.GetFieldValidations(), validationContext.GetBusinessRuleValidations()); } #region Notify changes bool hasNotifiedBusinessWarningsChanged = false; bool hasNotifiedBusinessErrorsChanged = false; foreach (var change in changes) { var changeAsFieldValidationResult = change.ValidationResult as IFieldValidationResult; var changeAsBusinessRuleValidationResult = change.ValidationResult as IBusinessRuleValidationResult; if (changeAsFieldValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: NotifyWarningsChanged(changeAsFieldValidationResult.PropertyName, false); break; case ValidationResultType.Error: NotifyErrorsChanged(changeAsFieldValidationResult.PropertyName, false); break; default: throw new ArgumentOutOfRangeException(); } } else if (changeAsBusinessRuleValidationResult != null) { switch (change.ValidationResult.ValidationResultType) { case ValidationResultType.Warning: if (!hasNotifiedBusinessWarningsChanged) { hasNotifiedBusinessWarningsChanged = true; NotifyWarningsChanged(string.Empty, false); } break; case ValidationResultType.Error: if (!hasNotifiedBusinessErrorsChanged) { hasNotifiedBusinessErrorsChanged = true; NotifyErrorsChanged(string.Empty, false); } break; default: throw new ArgumentOutOfRangeException(); } } } if (HasWarnings != hasWarnings) { RaisePropertyChanged("HasWarnings"); } if (HasErrors != hasErrors) { RaisePropertyChanged("HasErrors"); } #endregion IsValidating = false; }