Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved FluentValidation to standalone package #57

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions EasyWay.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyWay.Auth.WebApi", "sour
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyWay.AuthSamples", "samples\EasyWay.AuthSamples\EasyWay.AuthSamples.csproj", "{04307081-E971-4A1A-ACF2-D709EA7F8AD3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyWay.FluentValidation", "source\EasyWay.FluentValidation\EasyWay.FluentValidation.csproj", "{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{04307081-E971-4A1A-ACF2-D709EA7F8AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04307081-E971-4A1A-ACF2-D709EA7F8AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04307081-E971-4A1A-ACF2-D709EA7F8AD3}.Release|Any CPU.Build.0 = Release|Any CPU
{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -83,6 +89,7 @@ Global
{AE66C586-5C48-4A23-9186-63D90F0BC4A1} = {92D4A304-6772-4482-83CC-68FDF54087C4}
{2CF71C5B-0A95-416A-AB6A-6DB3545DCCF5} = {92D4A304-6772-4482-83CC-68FDF54087C4}
{04307081-E971-4A1A-ACF2-D709EA7F8AD3} = {B403E9EE-655A-481E-889B-F292845AE5BF}
{16C0EBA2-2E35-40E1-B655-70D6C16B2ECC} = {92D4A304-6772-4482-83CC-68FDF54087C4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {45C12B7C-F737-4C98-B967-CCB84BFB9080}
Expand Down
1 change: 1 addition & 0 deletions samples/EasyWay.Samples/EasyWay.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<ProjectReference Include="..\..\source\EasyWay.EntityFrameworkCore\EasyWay.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\source\EasyWay.FluentValidation\EasyWay.FluentValidation.csproj" />
<ProjectReference Include="..\..\source\EasyWay.WebApi\EasyWay.WebApi.csproj" />
<ProjectReference Include="..\..\source\EasyWay\EasyWay.csproj" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions samples/EasyWay.Samples/SampleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ protected override void ConfigureDependencies(IServiceCollection services, IConf
{
string connectionString = configuration.GetConnectionString("Database");

services.AddFluentValidation(Assemblies);

services.AddEntityFrameworkCore<SampleDbContext>(x => x.UseNpgsql(connectionString));
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
Expand Down
11 changes: 11 additions & 0 deletions source/EasyWay.FluentValidation/EasyWay.FluentValidation.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\EasyWay\EasyWay.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions source/EasyWay.FluentValidation/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using EasyWay.Internals;
using EasyWay.Internals.Validation;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace EasyWay
{
public static class Extensions
{
public static IServiceCollection AddFluentValidation(
this IServiceCollection services,
IEnumerable<Assembly> assemblies)
{
services.AddValidatorsFromAssemblies(assemblies);

services.AddScoped(typeof(IEasyWayValidator<>), typeof(FluentValidator<>));

return services;
}
}
}
42 changes: 42 additions & 0 deletions source/EasyWay.FluentValidation/Internals/FluentValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using EasyWay.Internals.Validation;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals
{
internal sealed class FluentValidator<T> : IEasyWayValidator<T>
{
private readonly IServiceProvider _serviceProvider;

public FluentValidator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IDictionary<string, string[]> Validate(T objectToValidate)
{
var validator = _serviceProvider.GetService<IValidator<T>>();

if (validator is null)
{
return new Dictionary<string, string[]>();
}

var result = validator.Validate(objectToValidate);

if (result.IsValid)
{
return new Dictionary<string, string[]>();
}

var errors = result.Errors
.GroupBy(x => x.PropertyName)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ErrorCode).ToArray()
);

return errors;
}
}
}
2 changes: 1 addition & 1 deletion source/EasyWay/EasyWay.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
Expand All @@ -16,6 +15,7 @@
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
<InternalsVisibleTo Include="$(AssemblyName).EntityFrameworkCore" />
<InternalsVisibleTo Include="$(AssemblyName).FluentValidation" />
<InternalsVisibleTo Include="$(AssemblyName).WebApi" />
</ItemGroup>
</Project>
15 changes: 4 additions & 11 deletions source/EasyWay/Internals/Commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using EasyWay.Internals.Contexts;
using FluentValidation;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Commands
Expand Down Expand Up @@ -28,21 +28,14 @@ public async Task<CommandResult> Execute<TCommand>(TCommand command, Cancellatio
{
_cancellationContextConstructor.Set(cancellationToken);

var validator = _serviceProvider.GetService<IValidator<TCommand>>();
var validator = _serviceProvider.GetService<IEasyWayValidator<TCommand>>();

if (validator is not null)
{
var result = validator.Validate(command);
var errors = validator.Validate(command);

if (!result.IsValid)
if (errors.Any())
{
var errors = result.Errors
.GroupBy(x => x.PropertyName)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ErrorCode).ToArray()
);

return CommandResult.Validation(errors);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EasyWay.Internals.Contexts;
using FluentValidation;
using FluentValidation.Results;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Commands
Expand Down Expand Up @@ -31,25 +30,18 @@ public async Task<CommandResult<TOperationResult>> Execute<TOperationResult>(Com

var commandType = command.GetType();

var validatorType = typeof(IValidator<>).MakeGenericType(commandType);
var validatorType = typeof(IEasyWayValidator<>).MakeGenericType(commandType);

var validator = _serviceProvider.GetService(validatorType);

if (validator is not null)
{
var result = (ValidationResult)validatorType
var errors = (IDictionary<string, string[]>)validatorType
.GetMethod("Validate")
?.Invoke(validator, [command]);

if (!result.IsValid)
if (errors.Any())
{
var errors = result.Errors
.GroupBy(x => x.PropertyName)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ErrorCode).ToArray()
);

return CommandResult<TOperationResult>.Validation(errors);
}
}
Expand Down
2 changes: 0 additions & 2 deletions source/EasyWay/Internals/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using EasyWay.Internals.Policies;
using EasyWay.Internals.Queries;
using EasyWay.Internals.Repositories;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

Expand All @@ -31,7 +30,6 @@ internal static void AddEasyWay(
.AddPolicies(assemblies)
.AddDomainServices(assemblies)
.AddFactories(assemblies)
.AddValidation(assemblies)
.AddInitializers(assemblies);
}
}
Expand Down
16 changes: 4 additions & 12 deletions source/EasyWay/Internals/Queries/QueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EasyWay.Internals.Contexts;
using FluentValidation;
using FluentValidation.Results;
using EasyWay.Internals.Validation;
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Queries
Expand All @@ -27,25 +26,18 @@

var queryType = query.GetType();

var validatorType = typeof(IValidator<>).MakeGenericType(queryType);
var validatorType = typeof(IEasyWayValidator<>).MakeGenericType(queryType);

var validator = _serviceProvider.GetService(validatorType);

if (validator is not null)
{
var result = (ValidationResult)validatorType
var errors = (IDictionary<string, string[]>)validatorType

Check warning on line 35 in source/EasyWay/Internals/Queries/QueryExecutor.cs

View workflow job for this annotation

GitHub Actions / Build & Tests

Converting null literal or possible null value to non-nullable type.
.GetMethod("Validate")
?.Invoke(validator, [query]);

if (!result.IsValid)
if (errors.Any())

Check warning on line 39 in source/EasyWay/Internals/Queries/QueryExecutor.cs

View workflow job for this annotation

GitHub Actions / Build & Tests

Possible null reference argument for parameter 'source' in 'bool Enumerable.Any<KeyValuePair<string, string[]>>(IEnumerable<KeyValuePair<string, string[]>> source)'.
{
var errors = result.Errors
.GroupBy(x => x.PropertyName)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ErrorCode).ToArray()
);

return QueryResult<TReadModel>.Validation(errors);
}
}
Expand All @@ -54,7 +46,7 @@

var queryHandler = _serviceProvider.GetRequiredService(queryHandlerType);

var queryResult = await (Task<QueryResult<TReadModel>>) queryHandlerType.GetMethod("Handle").Invoke(queryHandler, [query]);

Check warning on line 49 in source/EasyWay/Internals/Queries/QueryExecutor.cs

View workflow job for this annotation

GitHub Actions / Build & Tests

Dereference of a possibly null reference.

Check warning on line 49 in source/EasyWay/Internals/Queries/QueryExecutor.cs

View workflow job for this annotation

GitHub Actions / Build & Tests

Converting null literal or possible null value to non-nullable type.

return queryResult;
}
Expand Down
18 changes: 0 additions & 18 deletions source/EasyWay/Internals/Validation/Extensions.cs

This file was deleted.

7 changes: 7 additions & 0 deletions source/EasyWay/Internals/Validation/IEasyWayValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyWay.Internals.Validation
{
internal interface IEasyWayValidator<T>
{
IDictionary<string, string[]> Validate(T objectToValidate);
}
}
Loading