diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectValidator.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectValidator.cs index 0c60f59699..07d6556d10 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectValidator.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectValidator.cs @@ -9,6 +9,17 @@ namespace Volo.Abp.ObjectExtending { public static class ExtensibleObjectValidator { + [NotNull] + public static bool IsValid( + [NotNull] IHasExtraProperties extensibleObject, + [CanBeNull] ValidationContext objectValidationContext = null) + { + return GetValidationErrors( + extensibleObject, + objectValidationContext + ).Any(); + } + [NotNull] public static List GetValidationErrors( [NotNull] IHasExtraProperties extensibleObject, @@ -81,45 +92,87 @@ namespace Volo.Abp.ObjectExtending foreach (var property in properties) { - if (property.ValidationAttributes.Any()) - { - var propertyValidationContext = new ValidationContext(extensibleObject, objectValidationContext, null) - { - DisplayName = property.Name, - MemberName = property.Name - }; - - foreach (var attribute in property.ValidationAttributes) - { - var result = attribute.GetValidationResult( - extensibleObject.GetProperty(property.Name), - propertyValidationContext - ); - if (result != null) - { - validationErrors.Add(result); - } - } - } + AddPropertyValidationErrors(extensibleObject, validationErrors, objectValidationContext, property); + } + } - if (property.Validators.Any()) + private static void AddPropertyValidationErrors( + IHasExtraProperties extensibleObject, + List validationErrors, + ValidationContext objectValidationContext, + ObjectExtensionPropertyInfo property) + { + AddPropertyValidationAttributeErrors( + extensibleObject, + validationErrors, + objectValidationContext, + property + ); + + ExecuteCustomPropertyValidationActions( + extensibleObject, + validationErrors, + objectValidationContext, + property + ); + } + + private static void AddPropertyValidationAttributeErrors( + IHasExtraProperties extensibleObject, + List validationErrors, + ValidationContext objectValidationContext, + ObjectExtensionPropertyInfo property) + { + if (!property.ValidationAttributes.Any()) + { + return; + } + + var propertyValidationContext = new ValidationContext(extensibleObject, objectValidationContext, null) + { + DisplayName = property.Name, + MemberName = property.Name + }; + + foreach (var attribute in property.ValidationAttributes) + { + var result = attribute.GetValidationResult( + extensibleObject.GetProperty(property.Name), + propertyValidationContext + ); + + if (result != null) { - var context = new ObjectExtensionPropertyValidationContext( - property, - extensibleObject, - validationErrors, - objectValidationContext, - extensibleObject.GetProperty(property.Name) - ); - - foreach (var validator in property.Validators) - { - validator(context); - } + validationErrors.Add(result); } } } + private static void ExecuteCustomPropertyValidationActions( + IHasExtraProperties extensibleObject, + List validationErrors, + ValidationContext objectValidationContext, + ObjectExtensionPropertyInfo property) + { + if (!property.Validators.Any()) + { + return; + } + + var context = new ObjectExtensionPropertyValidationContext( + property, + extensibleObject, + validationErrors, + objectValidationContext, + extensibleObject.GetProperty(property.Name) + ); + + foreach (var validator in property.Validators) + { + validator(context); + } + } + private static void ExecuteCustomObjectValidationActions( IHasExtraProperties extensibleObject, List validationErrors,