diff --git a/CoreSharp.NHibernate.Generator/CoreSharp.NHibernate.Generator.csproj b/CoreSharp.NHibernate.Generator/CoreSharp.NHibernate.Generator.csproj index ea2a3df..a3bdf81 100644 --- a/CoreSharp.NHibernate.Generator/CoreSharp.NHibernate.Generator.csproj +++ b/CoreSharp.NHibernate.Generator/CoreSharp.NHibernate.Generator.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + net5.0 NHibernateGenerator diff --git a/CoreSharp.Validation.Abstractions/CoreSharp.Validation.Abstractions.csproj b/CoreSharp.Validation.Abstractions/CoreSharp.Validation.Abstractions.csproj index 9f5c4f4..594ec0f 100644 --- a/CoreSharp.Validation.Abstractions/CoreSharp.Validation.Abstractions.csproj +++ b/CoreSharp.Validation.Abstractions/CoreSharp.Validation.Abstractions.csproj @@ -2,6 +2,8 @@ netstandard2.0 + 9.0 + CoreSharp.Validation diff --git a/CoreSharp.Validation.Tests/CoreSharp.Validation.Tests.csproj b/CoreSharp.Validation.Tests/CoreSharp.Validation.Tests.csproj index 4891cfa..79762db 100644 --- a/CoreSharp.Validation.Tests/CoreSharp.Validation.Tests.csproj +++ b/CoreSharp.Validation.Tests/CoreSharp.Validation.Tests.csproj @@ -4,10 +4,12 @@ netcoreapp3.1 false + 9.0 + diff --git a/CoreSharp.Validation.Tests/Models/AsyncParent.cs b/CoreSharp.Validation.Tests/Models/AsyncParent.cs index d5b54e7..91ffb30 100644 --- a/CoreSharp.Validation.Tests/Models/AsyncParent.cs +++ b/CoreSharp.Validation.Tests/Models/AsyncParent.cs @@ -84,10 +84,11 @@ public override Task BeforeValidationAsync(TModel root, ValidationContext contex return Task.CompletedTask; } - public override Task ValidateAsync(TChild child, ValidationContext context) + public override IAsyncEnumerable ValidateAsync(TChild child, ValidationContext context) { ValidateModels.Add(new Tuple(child, this)); - return Task.FromResult(Success); + + return null; } public override Task CanValidateAsync(TChild child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/Models/GenericChildModel.cs b/CoreSharp.Validation.Tests/Models/GenericChildModel.cs index 3d7ce2c..496d98c 100644 --- a/CoreSharp.Validation.Tests/Models/GenericChildModel.cs +++ b/CoreSharp.Validation.Tests/Models/GenericChildModel.cs @@ -36,14 +36,14 @@ public GenericChildParentValidator(IValidator childValidator) public class GenericChildDomainValidator : AbstractDomainValidator where TChild : GenericChildChild { - public override ValidationFailure Validate(TChild child, ValidationContext context) + public override IEnumerable Validate(TChild child, ValidationContext context) { if (child.Name == "Invalid") { - return Failure("Invalid name", context); + yield return Failure("Invalid name", context); } - return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; + yield return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; } public override bool CanValidate(TChild child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/Models/GenericRootChildModel.cs b/CoreSharp.Validation.Tests/Models/GenericRootChildModel.cs index f89ce8c..ddb63ca 100644 --- a/CoreSharp.Validation.Tests/Models/GenericRootChildModel.cs +++ b/CoreSharp.Validation.Tests/Models/GenericRootChildModel.cs @@ -37,14 +37,14 @@ public class GenericRootChildDomainValidator : AbstractDomainVali where TRoot : GenericRootChildParent where TChild : GenericRootChildChild { - public override ValidationFailure Validate(TChild child, ValidationContext context) + public override IEnumerable Validate(TChild child, ValidationContext context) { if (child.Name == "Invalid") { - return Failure("Invalid name", context); + yield return Failure("Invalid name", context); } - return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; + yield return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; } public override bool CanValidate(TChild child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/Models/GenericRootModel.cs b/CoreSharp.Validation.Tests/Models/GenericRootModel.cs index d80d61d..d42f97c 100644 --- a/CoreSharp.Validation.Tests/Models/GenericRootModel.cs +++ b/CoreSharp.Validation.Tests/Models/GenericRootModel.cs @@ -26,14 +26,14 @@ public GenericRootModelValidator() public class GenericRootDomainValidator : AbstractDomainValidator where TRoot : GenericRootModel { - public override ValidationFailure Validate(TRoot child, ValidationContext context) + public override IEnumerable Validate(TRoot child, ValidationContext context) { if (child.Name == "Invalid") { - return Failure("Invalid name", context); + yield return Failure("Invalid name", context); } - return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; + yield return string.IsNullOrEmpty(child.Name) ? Failure(o => o.Name, "Should not be empty", context) : Success; } public override bool CanValidate(TRoot child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/Models/Parent.cs b/CoreSharp.Validation.Tests/Models/Parent.cs index d959a85..9f2f83d 100644 --- a/CoreSharp.Validation.Tests/Models/Parent.cs +++ b/CoreSharp.Validation.Tests/Models/Parent.cs @@ -63,7 +63,7 @@ public abstract class TestDomainValidator : TestDomainValidator< where TChild : class { } - public abstract class TestDomainValidator : AbstractDomainValidator + public abstract class TestDomainValidator : AbstractDomainValidator where TModel : class where TChild : class { @@ -82,10 +82,10 @@ public override void BeforeValidation(TModel root, ValidationContext context) BeforeValidationModels.Add(new Tuple(root, this)); } - public override ValidationFailure Validate(TChild child, ValidationContext context) + public override IEnumerable Validate(TChild child, ValidationContext context) { ValidateModels.Add(new Tuple(child, this)); - return Success; + yield break; } public override bool CanValidate(TChild child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/Models/TestModel.cs b/CoreSharp.Validation.Tests/Models/TestModel.cs index c309756..ebbff62 100644 --- a/CoreSharp.Validation.Tests/Models/TestModel.cs +++ b/CoreSharp.Validation.Tests/Models/TestModel.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using FluentValidation; using FluentValidation.Results; @@ -21,17 +22,18 @@ public class TestModelDomainValidator : AbstractDomainValidator { public static int ValidateCount; public static int CanValidateCount; - public static int ValidatBeforeValidationCount; + public static int ValidateBeforeValidationCount; public override void BeforeValidation(TestModel root, ValidationContext context) { - ValidatBeforeValidationCount++; + ValidateBeforeValidationCount++; } - public override ValidationFailure Validate(TestModel child, ValidationContext context) + public override IEnumerable Validate(TestModel child, ValidationContext context) { ValidateCount++; - return Success; + + yield break; } public override bool CanValidate(TestModel child, ValidationContext context) @@ -47,18 +49,19 @@ public class TestModelAsyncDomainValidator : AbstractAsyncDomainValidator ValidateAsync(TestModel child, ValidationContext context) + public override IAsyncEnumerable ValidateAsync(TestModel child, ValidationContext context) { ValidateCount++; - return Task.FromResult(Success); + + return null; } public override Task CanValidateAsync(TestModel child, ValidationContext context) diff --git a/CoreSharp.Validation.Tests/ValidationTests.cs b/CoreSharp.Validation.Tests/ValidationTests.cs index 2020382..cd9e8fa 100644 --- a/CoreSharp.Validation.Tests/ValidationTests.cs +++ b/CoreSharp.Validation.Tests/ValidationTests.cs @@ -27,10 +27,10 @@ public void ValidateWithDomainValidator() { TestModelDomainValidator.ValidateCount = 0; TestModelDomainValidator.CanValidateCount = 0; - TestModelDomainValidator.ValidatBeforeValidationCount = 0; + TestModelDomainValidator.ValidateBeforeValidationCount = 0; TestModelAsyncDomainValidator.ValidateCount = 0; TestModelAsyncDomainValidator.CanValidateCount = 0; - TestModelAsyncDomainValidator.ValidatBeforeValidationCount = 0; + TestModelAsyncDomainValidator.ValidateBeforeValidationCount = 0; var validator = Container.GetInstance>(); for (var i = 0; i < 5; i++) @@ -40,10 +40,10 @@ public void ValidateWithDomainValidator() Assert.True(valResult.IsValid); Assert.Equal(i + 1, TestModelDomainValidator.ValidateCount); Assert.Equal(i + 1, TestModelDomainValidator.CanValidateCount); - Assert.Equal(i + 1, TestModelDomainValidator.ValidatBeforeValidationCount); + Assert.Equal(i + 1, TestModelDomainValidator.ValidateBeforeValidationCount); Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidateCount); Assert.Equal(i + 1, TestModelAsyncDomainValidator.CanValidateCount); - Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidatBeforeValidationCount); + Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidateBeforeValidationCount); } } @@ -52,10 +52,10 @@ public async Task ValidateWithAsyncDomainValidator() { TestModelDomainValidator.ValidateCount = 0; TestModelDomainValidator.CanValidateCount = 0; - TestModelDomainValidator.ValidatBeforeValidationCount = 0; + TestModelDomainValidator.ValidateBeforeValidationCount = 0; TestModelAsyncDomainValidator.ValidateCount = 0; TestModelAsyncDomainValidator.CanValidateCount = 0; - TestModelAsyncDomainValidator.ValidatBeforeValidationCount = 0; + TestModelAsyncDomainValidator.ValidateBeforeValidationCount = 0; var validator = Container.GetInstance>(); for (var i = 0; i < 5; i++) @@ -65,10 +65,10 @@ public async Task ValidateWithAsyncDomainValidator() Assert.True(valResult.IsValid); Assert.Equal(i + 1, TestModelDomainValidator.ValidateCount); Assert.Equal(i + 1, TestModelDomainValidator.CanValidateCount); - Assert.Equal(i + 1, TestModelDomainValidator.ValidatBeforeValidationCount); + Assert.Equal(i + 1, TestModelDomainValidator.ValidateBeforeValidationCount); Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidateCount); Assert.Equal(i + 1, TestModelAsyncDomainValidator.CanValidateCount); - Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidatBeforeValidationCount); + Assert.Equal(i + 1, TestModelAsyncDomainValidator.ValidateBeforeValidationCount); } } diff --git a/CoreSharp.Validation/AbstractAsyncDomainValidator.cs b/CoreSharp.Validation/AbstractAsyncDomainValidator.cs index d7ec937..42dfec8 100644 --- a/CoreSharp.Validation/AbstractAsyncDomainValidator.cs +++ b/CoreSharp.Validation/AbstractAsyncDomainValidator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using CoreSharp.Validation.Internal; @@ -15,7 +16,7 @@ public abstract class AbstractAsyncDomainValidator : IAsyncDomain { public virtual string[] RuleSets { get; } - public abstract Task ValidateAsync(TChild model, ValidationContext context); + public abstract IAsyncEnumerable ValidateAsync(TChild model, ValidationContext context); public virtual Task CanValidateAsync(TChild model, ValidationContext context) { @@ -27,7 +28,7 @@ public virtual Task BeforeValidationAsync(TRoot root, ValidationContext context) return Task.CompletedTask; } - Task IAsyncDomainValidator.ValidateAsync(object model, ValidationContext context) + IAsyncEnumerable IAsyncDomainValidator.ValidateAsync(object model, ValidationContext context) { return ValidateAsync((TChild)model, context); } @@ -51,7 +52,5 @@ protected ValidationFailure Failure(string errorMessage, ValidationContext conte { return DomainValidationContext.CreateValidationFailure(errorMessage, context); } - - protected ValidationFailure Success => null; } } diff --git a/CoreSharp.Validation/AbstractAsyncPropertyDomainValidator.cs b/CoreSharp.Validation/AbstractAsyncPropertyDomainValidator.cs index d490ef6..e18e7bf 100644 --- a/CoreSharp.Validation/AbstractAsyncPropertyDomainValidator.cs +++ b/CoreSharp.Validation/AbstractAsyncPropertyDomainValidator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using CoreSharp.Validation; @@ -9,7 +10,7 @@ namespace CoreSharp.Validation { public abstract class AbstractAsyncPropertyDomainValidator : AbstractAsyncDomainValidator { - public override async Task ValidateAsync(TModel model, ValidationContext context) + public override async IAsyncEnumerable ValidateAsync(TModel model, ValidationContext context) { var propGetter = PropertyExpression.Compile(); var propVal = propGetter(model); @@ -26,10 +27,8 @@ public override async Task ValidateAsync(TModel model, Valida if (!valid) { - return Failure(PropertyExpression, ErrorTemplate, context); + yield return Failure(PropertyExpression, ErrorTemplate, context); } - - return Success; } protected abstract string ErrorTemplate { get; } diff --git a/CoreSharp.Validation/AbstractDomainValidator.cs b/CoreSharp.Validation/AbstractDomainValidator.cs index e1b0768..96d691a 100644 --- a/CoreSharp.Validation/AbstractDomainValidator.cs +++ b/CoreSharp.Validation/AbstractDomainValidator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; using CoreSharp.Validation.Internal; using FluentValidation; @@ -14,7 +15,7 @@ public abstract class AbstractDomainValidator : IDomainValidator< { public virtual string[] RuleSets { get; } - public abstract ValidationFailure Validate(TChild model, ValidationContext context); + public abstract IEnumerable Validate(TChild model, ValidationContext context); public virtual bool CanValidate(TChild model, ValidationContext context) { @@ -25,7 +26,7 @@ public virtual void BeforeValidation(TRoot root, ValidationContext context) { } - ValidationFailure IDomainValidator.Validate(object model, ValidationContext context) + IEnumerable IDomainValidator.Validate(object model, ValidationContext context) { return Validate((TChild)model, context); } diff --git a/CoreSharp.Validation/CoreSharp.Validation.csproj b/CoreSharp.Validation/CoreSharp.Validation.csproj index 95fa26a..945b610 100644 --- a/CoreSharp.Validation/CoreSharp.Validation.csproj +++ b/CoreSharp.Validation/CoreSharp.Validation.csproj @@ -3,6 +3,7 @@ netstandard2.0 CoreSharp.Validation + 9.0 cime .NET standard implementation of validation diff --git a/CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs b/CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs index 8031b2e..618551e 100644 --- a/CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs +++ b/CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using CoreSharp.Common.Models; using CoreSharp.Validation; +using CoreSharp.Validation.PropertyValidators; +// ReSharper disable once CheckNamespace namespace FluentValidation { public static class CoreSharpFluentValidationExtensions diff --git a/CoreSharp.Validation/IAsyncDomainValidator.cs b/CoreSharp.Validation/IAsyncDomainValidator.cs index 565bebe..fabf0fd 100644 --- a/CoreSharp.Validation/IAsyncDomainValidator.cs +++ b/CoreSharp.Validation/IAsyncDomainValidator.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using FluentValidation; using FluentValidation.Results; @@ -8,7 +9,7 @@ public interface IAsyncDomainValidator { Task BeforeValidationAsync(object model, ValidationContext context); - Task ValidateAsync(object model, ValidationContext context); + IAsyncEnumerable ValidateAsync(object model, ValidationContext context); Task CanValidateAsync(object model, ValidationContext context); @@ -23,7 +24,7 @@ public interface IAsyncDomainValidator : IAsyncDomainValidator { Task BeforeValidationAsync(TRoot root, ValidationContext context); - Task ValidateAsync(TChild child, ValidationContext context); + IAsyncEnumerable ValidateAsync(TChild child, ValidationContext context); Task CanValidateAsync(TChild child, ValidationContext context); } diff --git a/CoreSharp.Validation/IDomainValidator.cs b/CoreSharp.Validation/IDomainValidator.cs index 906248d..31d749b 100644 --- a/CoreSharp.Validation/IDomainValidator.cs +++ b/CoreSharp.Validation/IDomainValidator.cs @@ -1,4 +1,5 @@ -using FluentValidation; +using System.Collections.Generic; +using FluentValidation; using FluentValidation.Results; namespace CoreSharp.Validation @@ -7,7 +8,7 @@ public interface IDomainValidator { void BeforeValidation(object model, ValidationContext context); - ValidationFailure Validate(object model, ValidationContext context); + IEnumerable Validate(object model, ValidationContext context); bool CanValidate(object model, ValidationContext context); @@ -22,7 +23,7 @@ public interface IDomainValidator : IDomainValidator { void BeforeValidation(TRoot root, ValidationContext context); - ValidationFailure Validate(TChild child, ValidationContext context); + IEnumerable Validate(TChild child, ValidationContext context); bool CanValidate(TChild child, ValidationContext context); } diff --git a/CoreSharp.Validation/Internal/DomainValidatorsValidator.cs b/CoreSharp.Validation/Internal/DomainValidatorsValidator.cs index e953944..e85326c 100644 --- a/CoreSharp.Validation/Internal/DomainValidatorsValidator.cs +++ b/CoreSharp.Validation/Internal/DomainValidatorsValidator.cs @@ -41,24 +41,28 @@ public async Task> ValidateAsync(ValidationContex continue; } - var result = await rule.ValidateAsync(context.InstanceToValidate, context); - if (result == null) + var results = rule.ValidateAsync(context.InstanceToValidate, context); + + if (results == null) { continue; } - list.Add(result); + await foreach (var result in results) + { + list.Add(result); + } } foreach (var rule in _rulesFunc(context).Where(o => o.CanValidate(context.InstanceToValidate, context))) { - var result = rule.Validate(context.InstanceToValidate, context); - if (result == null) + var results = rule.Validate(context.InstanceToValidate, context); + if (results == null) { continue; } - list.Add(result); + list.AddRange(results); } return list; diff --git a/CoreSharp.Validation/PropertyValidators/GuidValidator.cs b/CoreSharp.Validation/PropertyValidators/GuidValidator.cs index f657cff..79d91aa 100644 --- a/CoreSharp.Validation/PropertyValidators/GuidValidator.cs +++ b/CoreSharp.Validation/PropertyValidators/GuidValidator.cs @@ -2,7 +2,7 @@ using FluentValidation.Resources; using FluentValidation.Validators; -namespace CoreSharp.Validation +namespace CoreSharp.Validation.PropertyValidators { public class GuidValidator : AbstractCommonPropertyValidator { diff --git a/CoreSharp.Validation/PropertyValidators/OneOfValidator.cs b/CoreSharp.Validation/PropertyValidators/OneOfValidator.cs index 1d5aaf7..2a1de0f 100644 --- a/CoreSharp.Validation/PropertyValidators/OneOfValidator.cs +++ b/CoreSharp.Validation/PropertyValidators/OneOfValidator.cs @@ -4,7 +4,7 @@ using FluentValidation.Resources; using FluentValidation.Validators; -namespace CoreSharp.Validation +namespace CoreSharp.Validation.PropertyValidators { public class OneOfValidator : AbstractCommonPropertyValidator diff --git a/CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.cs b/CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.cs index 1ea9c40..54f2698 100644 --- a/CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.cs +++ b/CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.cs @@ -2,7 +2,7 @@ using FluentValidation.Resources; using FluentValidation.Validators; -namespace CoreSharp.Validation +namespace CoreSharp.Validation.PropertyValidators { public class PhoneNumberValidator : AbstractCommonPropertyValidator