-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
769d1b9
commit 58d2646
Showing
12 changed files
with
267 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 23 additions & 16 deletions
39
...Validation/BitzArt.FluentValidation.Extensions/BitzArt.FluentValidation.Extensions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>FluentValidation</RootNamespace> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>FluentValidation</RootNamespace> | ||
|
||
<PackageId>BitzArt.FluentValidation.Extensions</PackageId> | ||
<Authors>BitzArt</Authors> | ||
<Description>Extensions for FluentValidation</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/BitzArt/Miscellaneous</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl> | ||
</PropertyGroup> | ||
<PackageId>BitzArt.FluentValidation.Extensions</PackageId> | ||
<Authors>BitzArt</Authors> | ||
<Description>Extensions for FluentValidation</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/BitzArt/Miscellaneous</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentValidation" Version="11.5.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="FluentValidation" Version="11.8.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo"> | ||
<_Parameter1>BitzArt.FluentValidation.Extensions.UnitTests</_Parameter1> | ||
</AssemblyAttribute> | ||
</ItemGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
src/FluentValidation/BitzArt.FluentValidation.Extensions/Enums/ActionType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace FluentValidation; | ||
|
||
public enum ActionType : byte | ||
{ | ||
Get = 1, | ||
Create = 2, | ||
Update = 3, | ||
Patch = 4, | ||
Options = 5, | ||
Delete = 6 | ||
} |
46 changes: 46 additions & 0 deletions
46
...Validation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Reflection; | ||
|
||
namespace FluentValidation; | ||
|
||
public static class AddActionValidatorsExtension | ||
{ | ||
public static IServiceCollection AddActionValidatorsFromAssemblyContaining<TAssemblyPointer>(this IServiceCollection services, Func<IServiceProvider, ActionType> getActionType) | ||
=> services.AddActionValidatorsFromAssemblyContaining(typeof(TAssemblyPointer), getActionType); | ||
|
||
public static IServiceCollection AddActionValidatorsFromAssemblyContaining(this IServiceCollection services, Type type, Func<IServiceProvider, ActionType> getActionType) | ||
=> services.AddActionValidatorsFromAssembly(type.Assembly, getActionType); | ||
|
||
public static IServiceCollection AddActionValidatorsFromAssembly(this IServiceCollection services, Assembly assembly, Func<IServiceProvider, ActionType> getActionType) | ||
{ | ||
var validators = assembly | ||
.DefinedTypes | ||
.Where(x => x.BaseType is not null && x.BaseType.IsGenericType) | ||
.Where(x => x.BaseType!.GetGenericTypeDefinition() == typeof(ActionValidator<>)); | ||
|
||
foreach (var validator in validators) services.AddActionValidator(validator, getActionType); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddActionValidator(this IServiceCollection services, Type validatorType, Func<IServiceProvider, ActionType> getActionType) | ||
{ | ||
if (validatorType is null) throw new ArgumentException($"{nameof(validatorType)} must not be null"); | ||
if (validatorType.BaseType!.GetGenericTypeDefinition() != typeof(ActionValidator<>)) throw new ArgumentException($"{validatorType.Name} is not assignable to ActionValidator"); | ||
|
||
var baseClassDefinition = validatorType.BaseType!; | ||
var validationObjectType = baseClassDefinition.GetGenericArguments().Single(); | ||
|
||
var registrationType = typeof(IValidator<>).MakeGenericType(validationObjectType); | ||
|
||
services.AddTransient(validatorType); | ||
services.AddScoped(registrationType, x => | ||
{ | ||
var validator = x.GetRequiredService(validatorType); | ||
(validator as IActionValidator)!.ActionType = getActionType.Invoke(x); | ||
return validator; | ||
}); | ||
|
||
return services; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/FluentValidation/BitzArt.FluentValidation.Extensions/Interfaces/IActionValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentValidation; | ||
|
||
internal interface IActionValidator | ||
{ | ||
public ActionType ActionType { get; set; } | ||
} |
24 changes: 9 additions & 15 deletions
24
src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/ActionValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
namespace FluentValidation; | ||
|
||
public abstract class ActionValidator<T> : JsonValidator<T> | ||
public abstract class ActionValidator<T> : AbstractValidator<T>, IActionValidator | ||
{ | ||
protected readonly ActionType ActionType; | ||
|
||
protected ActionValidator(ActionType actionType) | ||
private ActionType? _actionType; | ||
public ActionType ActionType | ||
{ | ||
ActionType = actionType; | ||
get => _actionType is not null ? | ||
_actionType!.Value : | ||
throw new ArgumentException("ActionType is not configured for this ActionValidator."); | ||
|
||
set => _actionType = value; | ||
} | ||
|
||
public IConditionBuilder When(ActionType actionType, Action action) | ||
=> When(x => ActionType == actionType, action); | ||
|
||
public IConditionBuilder Unless(ActionType actionType, Action action) | ||
=> Unless(x => ActionType == actionType, action); | ||
} | ||
|
||
public enum ActionType : byte | ||
{ | ||
Get = 1, | ||
Create = 2, | ||
Update = 3, | ||
Patch = 4, | ||
Options = 5, | ||
Delete = 6 | ||
} |
44 changes: 0 additions & 44 deletions
44
src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/JsonValidator.cs
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...luentValidation.Extensions.UnitTests/BitzArt.FluentValidation.Extensions.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>FluentValidation</RootNamespace> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\FluentValidation\BitzArt.FluentValidation.Extensions\BitzArt.FluentValidation.Extensions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
6 changes: 6 additions & 0 deletions
6
tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Models/TestEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentValidation; | ||
|
||
public class TestEntity | ||
{ | ||
public string? Name { get; set; } | ||
} |
5 changes: 5 additions & 0 deletions
5
...entValidation/BitzArt.FluentValidation.Extensions.UnitTests/Models/TestEntityValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace FluentValidation.Models; | ||
|
||
public class TestEntityValidator : ActionValidator<TestEntity> | ||
{ | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Models/TestOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentValidation; | ||
|
||
internal class TestOptions(ActionType actionType) | ||
{ | ||
public ActionType ActionType { get; set; } = actionType; | ||
} |
110 changes: 110 additions & 0 deletions
110
.../BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorsExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using FluentValidation.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FluentValidation; | ||
|
||
public class AddActionValidatorsExtensionTests | ||
{ | ||
[Theory] | ||
[InlineData(ActionType.Get)] | ||
[InlineData(ActionType.Create)] | ||
[InlineData(ActionType.Patch)] | ||
[InlineData(ActionType.Update)] | ||
public void AddActionValidator_WithFlatActionType_AddsValidator(ActionType actionType) | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
var validatorType = typeof(TestEntityValidator); | ||
|
||
services.AddActionValidator(validatorType, x => actionType); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var validator = serviceProvider.GetService<IValidator<TestEntity>>(); | ||
|
||
Assert.NotNull(validator); | ||
Assert.True(validator is TestEntityValidator); | ||
var validatorCasted = (TestEntityValidator)validator; | ||
Assert.Equal(actionType, validatorCasted.ActionType); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ActionType.Get)] | ||
[InlineData(ActionType.Create)] | ||
[InlineData(ActionType.Patch)] | ||
[InlineData(ActionType.Update)] | ||
public void AddActionValidator_WithActionTypeFromDi_AddsValidator(ActionType actionType) | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
var validatorType = typeof(TestEntityValidator); | ||
|
||
var options = new TestOptions(actionType); | ||
services.AddSingleton(options); | ||
services.AddActionValidator(validatorType, x => x.GetRequiredService<TestOptions>().ActionType); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var validator = serviceProvider.GetService<IValidator<TestEntity>>(); | ||
|
||
Assert.NotNull(validator); | ||
Assert.True(validator is TestEntityValidator); | ||
var validatorCasted = (TestEntityValidator)validator; | ||
Assert.Equal(actionType, validatorCasted.ActionType); | ||
} | ||
|
||
[Fact] | ||
public void AddActionValidatorsFromAssembly_WithFlatActionType_AddsTestEntityValidator() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
var validatorType = typeof(TestEntityValidator); | ||
var actionType = ActionType.Get; | ||
|
||
services.AddActionValidatorsFromAssembly(typeof(AddActionValidatorsExtensionTests).Assembly, x => actionType); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var validator = serviceProvider.GetService<IValidator<TestEntity>>(); | ||
|
||
Assert.NotNull(validator); | ||
Assert.True(validator is TestEntityValidator); | ||
var validatorCasted = (TestEntityValidator)validator; | ||
Assert.Equal(actionType, validatorCasted.ActionType); | ||
} | ||
|
||
[Fact] | ||
public void AddActionValidatorsFromAssemblyContaining_WithFlatActionType_AddsTestEntityValidator() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
var validatorType = typeof(TestEntityValidator); | ||
var actionType = ActionType.Get; | ||
|
||
services.AddActionValidatorsFromAssemblyContaining(typeof(AddActionValidatorsExtensionTests), x => actionType); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var validator = serviceProvider.GetService<IValidator<TestEntity>>(); | ||
|
||
Assert.NotNull(validator); | ||
Assert.True(validator is TestEntityValidator); | ||
var validatorCasted = (TestEntityValidator)validator; | ||
Assert.Equal(actionType, validatorCasted.ActionType); | ||
} | ||
|
||
[Fact] | ||
public void AddActionValidatorsFromAssemblyContainingGeneric_WithFlatActionType_AddsTestEntityValidator() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
var validatorType = typeof(TestEntityValidator); | ||
var actionType = ActionType.Get; | ||
|
||
services.AddActionValidatorsFromAssemblyContaining<AddActionValidatorsExtensionTests>(x => actionType); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var validator = serviceProvider.GetService<IValidator<TestEntity>>(); | ||
|
||
Assert.NotNull(validator); | ||
Assert.True(validator is TestEntityValidator); | ||
var validatorCasted = (TestEntityValidator)validator; | ||
Assert.Equal(actionType, validatorCasted.ActionType); | ||
} | ||
} |