Skip to content

Commit

Permalink
Merge pull request #12 from cime/validation_enumerable
Browse files Browse the repository at this point in the history
change: domain validator - enumerable ValidationFailure-s
  • Loading branch information
cime authored and web-flow committed Mar 1, 2021
2 parents 9d6efcd + 7a87301 commit 8e7ed47
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>NHibernateGenerator</AssemblyName>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<RootNamespace>CoreSharp.Validation</RootNamespace>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions CoreSharp.Validation.Tests/CoreSharp.Validation.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
5 changes: 3 additions & 2 deletions CoreSharp.Validation.Tests/Models/AsyncParent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public override Task BeforeValidationAsync(TModel root, ValidationContext contex
return Task.CompletedTask;
}

public override Task<ValidationFailure> ValidateAsync(TChild child, ValidationContext context)
public override IAsyncEnumerable<ValidationFailure> ValidateAsync(TChild child, ValidationContext context)
{
ValidateModels.Add(new Tuple<TChild, IAsyncDomainValidator>(child, this));
return Task.FromResult(Success);

return null;
}

public override Task<bool> CanValidateAsync(TChild child, ValidationContext context)
Expand Down
6 changes: 3 additions & 3 deletions CoreSharp.Validation.Tests/Models/GenericChildModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public GenericChildParentValidator(IValidator<GenericChildChild> childValidator)

public class GenericChildDomainValidator<TChild> : AbstractDomainValidator<GenericChildParent, TChild> where TChild : GenericChildChild
{
public override ValidationFailure Validate(TChild child, ValidationContext context)
public override IEnumerable<ValidationFailure> 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)
Expand Down
6 changes: 3 additions & 3 deletions CoreSharp.Validation.Tests/Models/GenericRootChildModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class GenericRootChildDomainValidator<TRoot, TChild> : AbstractDomainVali
where TRoot : GenericRootChildParent
where TChild : GenericRootChildChild
{
public override ValidationFailure Validate(TChild child, ValidationContext context)
public override IEnumerable<ValidationFailure> 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)
Expand Down
6 changes: 3 additions & 3 deletions CoreSharp.Validation.Tests/Models/GenericRootModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public GenericRootModelValidator()
public class GenericRootDomainValidator<TRoot> : AbstractDomainValidator<TRoot>
where TRoot : GenericRootModel
{
public override ValidationFailure Validate(TRoot child, ValidationContext context)
public override IEnumerable<ValidationFailure> 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)
Expand Down
6 changes: 3 additions & 3 deletions CoreSharp.Validation.Tests/Models/Parent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public abstract class TestDomainValidator<TModel, TChild> : TestDomainValidator<
where TChild : class
{ }

public abstract class TestDomainValidator<TModel, TChild, TType> : AbstractDomainValidator<TModel, TChild>
public abstract class TestDomainValidator<TModel, TChild, TType> : AbstractDomainValidator<TModel, TChild>
where TModel : class
where TChild : class
{
Expand All @@ -82,10 +82,10 @@ public override void BeforeValidation(TModel root, ValidationContext context)
BeforeValidationModels.Add(new Tuple<TModel, IDomainValidator>(root, this));
}

public override ValidationFailure Validate(TChild child, ValidationContext context)
public override IEnumerable<ValidationFailure> Validate(TChild child, ValidationContext context)
{
ValidateModels.Add(new Tuple<TChild, IDomainValidator>(child, this));
return Success;
yield break;
}

public override bool CanValidate(TChild child, ValidationContext context)
Expand Down
21 changes: 12 additions & 9 deletions CoreSharp.Validation.Tests/Models/TestModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;

Expand All @@ -21,17 +22,18 @@ public class TestModelDomainValidator : AbstractDomainValidator<TestModel>
{
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<ValidationFailure> Validate(TestModel child, ValidationContext context)
{
ValidateCount++;
return Success;

yield break;
}

public override bool CanValidate(TestModel child, ValidationContext context)
Expand All @@ -47,18 +49,19 @@ public class TestModelAsyncDomainValidator : AbstractAsyncDomainValidator<TestMo
{
public static int ValidateCount;
public static int CanValidateCount;
public static int ValidatBeforeValidationCount;
public static int ValidateBeforeValidationCount;

public override Task BeforeValidationAsync(TestModel root, ValidationContext context)
{
ValidatBeforeValidationCount++;
ValidateBeforeValidationCount++;
return Task.CompletedTask;
}

public override Task<ValidationFailure> ValidateAsync(TestModel child, ValidationContext context)
public override IAsyncEnumerable<ValidationFailure> ValidateAsync(TestModel child, ValidationContext context)
{
ValidateCount++;
return Task.FromResult(Success);

return null;
}

public override Task<bool> CanValidateAsync(TestModel child, ValidationContext context)
Expand Down
16 changes: 8 additions & 8 deletions CoreSharp.Validation.Tests/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IValidator<TestModel>>();

for (var i = 0; i < 5; i++)
Expand All @@ -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);
}
}

Expand All @@ -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<IValidator<TestModel>>();

for (var i = 0; i < 5; i++)
Expand All @@ -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);
}
}

Expand Down
7 changes: 3 additions & 4 deletions CoreSharp.Validation/AbstractAsyncDomainValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreSharp.Validation.Internal;
Expand All @@ -15,7 +16,7 @@ public abstract class AbstractAsyncDomainValidator<TRoot, TChild> : IAsyncDomain
{
public virtual string[] RuleSets { get; }

public abstract Task<ValidationFailure> ValidateAsync(TChild model, ValidationContext context);
public abstract IAsyncEnumerable<ValidationFailure> ValidateAsync(TChild model, ValidationContext context);

public virtual Task<bool> CanValidateAsync(TChild model, ValidationContext context)
{
Expand All @@ -27,7 +28,7 @@ public virtual Task BeforeValidationAsync(TRoot root, ValidationContext context)
return Task.CompletedTask;
}

Task<ValidationFailure> IAsyncDomainValidator.ValidateAsync(object model, ValidationContext context)
IAsyncEnumerable<ValidationFailure> IAsyncDomainValidator.ValidateAsync(object model, ValidationContext context)
{
return ValidateAsync((TChild)model, context);
}
Expand All @@ -51,7 +52,5 @@ protected ValidationFailure Failure(string errorMessage, ValidationContext conte
{
return DomainValidationContext.CreateValidationFailure(errorMessage, context);
}

protected ValidationFailure Success => null;
}
}
7 changes: 3 additions & 4 deletions CoreSharp.Validation/AbstractAsyncPropertyDomainValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreSharp.Validation;
Expand All @@ -9,7 +10,7 @@ namespace CoreSharp.Validation
{
public abstract class AbstractAsyncPropertyDomainValidator<TModel, TProp> : AbstractAsyncDomainValidator<TModel>
{
public override async Task<ValidationFailure> ValidateAsync(TModel model, ValidationContext context)
public override async IAsyncEnumerable<ValidationFailure> ValidateAsync(TModel model, ValidationContext context)
{
var propGetter = PropertyExpression.Compile();
var propVal = propGetter(model);
Expand All @@ -26,10 +27,8 @@ public override async Task<ValidationFailure> ValidateAsync(TModel model, Valida

if (!valid)
{
return Failure(PropertyExpression, ErrorTemplate, context);
yield return Failure(PropertyExpression, ErrorTemplate, context);
}

return Success;
}

protected abstract string ErrorTemplate { get; }
Expand Down
5 changes: 3 additions & 2 deletions CoreSharp.Validation/AbstractDomainValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using CoreSharp.Validation.Internal;
using FluentValidation;
Expand All @@ -14,7 +15,7 @@ public abstract class AbstractDomainValidator<TRoot, TChild> : IDomainValidator<
{
public virtual string[] RuleSets { get; }

public abstract ValidationFailure Validate(TChild model, ValidationContext context);
public abstract IEnumerable<ValidationFailure> Validate(TChild model, ValidationContext context);

public virtual bool CanValidate(TChild model, ValidationContext context)
{
Expand All @@ -25,7 +26,7 @@ public virtual void BeforeValidation(TRoot root, ValidationContext context)
{
}

ValidationFailure IDomainValidator.Validate(object model, ValidationContext context)
IEnumerable<ValidationFailure> IDomainValidator.Validate(object model, ValidationContext context)
{
return Validate((TChild)model, context);
}
Expand Down
1 change: 1 addition & 0 deletions CoreSharp.Validation/CoreSharp.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>CoreSharp.Validation</PackageId>
<LangVersion>9.0</LangVersion>

<Authors>cime</Authors>
<Description>.NET standard implementation of validation</Description>
Expand Down
2 changes: 2 additions & 0 deletions CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions CoreSharp.Validation/IAsyncDomainValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;

Expand All @@ -8,7 +9,7 @@ public interface IAsyncDomainValidator
{
Task BeforeValidationAsync(object model, ValidationContext context);

Task<ValidationFailure> ValidateAsync(object model, ValidationContext context);
IAsyncEnumerable<ValidationFailure> ValidateAsync(object model, ValidationContext context);

Task<bool> CanValidateAsync(object model, ValidationContext context);

Expand All @@ -23,7 +24,7 @@ public interface IAsyncDomainValidator<TRoot, TChild> : IAsyncDomainValidator
{
Task BeforeValidationAsync(TRoot root, ValidationContext context);

Task<ValidationFailure> ValidateAsync(TChild child, ValidationContext context);
IAsyncEnumerable<ValidationFailure> ValidateAsync(TChild child, ValidationContext context);

Task<bool> CanValidateAsync(TChild child, ValidationContext context);
}
Expand Down
7 changes: 4 additions & 3 deletions CoreSharp.Validation/IDomainValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentValidation;
using System.Collections.Generic;
using FluentValidation;
using FluentValidation.Results;

namespace CoreSharp.Validation
Expand All @@ -7,7 +8,7 @@ public interface IDomainValidator
{
void BeforeValidation(object model, ValidationContext context);

ValidationFailure Validate(object model, ValidationContext context);
IEnumerable<ValidationFailure> Validate(object model, ValidationContext context);

bool CanValidate(object model, ValidationContext context);

Expand All @@ -22,7 +23,7 @@ public interface IDomainValidator<TRoot, TChild> : IDomainValidator
{
void BeforeValidation(TRoot root, ValidationContext context);

ValidationFailure Validate(TChild child, ValidationContext context);
IEnumerable<ValidationFailure> Validate(TChild child, ValidationContext context);

bool CanValidate(TChild child, ValidationContext context);
}
Expand Down
Loading

0 comments on commit 8e7ed47

Please sign in to comment.