Skip to content
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
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
[*]
indent_style = space
vsspell_section_id = cb4a4387061549238f01a4c2739f296e
vsspell_ignored_keywords_cb4a4387061549238f01a4c2739f296e = Mongo
vsspell_ignored_keywords_cb4a4387061549238f01a4c2739f296e = Mongo|validators

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
Expand Down Expand Up @@ -141,4 +141,4 @@ csharp_preserve_single_line_blocks = true
###############################
[*.vb]
# Modifier preferences
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PackageVersion Include="Autofac" Version="7.0.1" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="FluentValidation" Version="11.6.0" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="IdGen" Version="3.0.3" />
<PackageVersion Include="IdGen.DependencyInjection" Version="3.0.0" />
<PackageVersion Include="MediatR" Version="12.1.1" />
Expand Down
1 change: 1 addition & 0 deletions src/API.Core/API.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Autofac" />
<PackageReference Include="FluentValidation" />
<PackageReference Include="IdGen" />
<PackageReference Include="MediatR" />
<PackageReference Include="TIKSN-Framework" />
Expand Down
31 changes: 31 additions & 0 deletions src/API.Core/Validators/ValidationBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FluentValidation;
using MediatR;

namespace Fossa.API.Core.Validators;

public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;

public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}

public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (_validators.Any())
{
var context = new ValidationContext<TRequest>(request);
var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken))).ConfigureAwait(false);
var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList();
if (failures.Count != 0)
throw new ValidationException(failures);
}

return await next().ConfigureAwait(false);
}
}
1 change: 1 addition & 0 deletions src/API.Web/API.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" />
<PackageReference Include="IdGen.DependencyInjection" />
<PackageReference Include="MediatR" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
Expand Down
15 changes: 11 additions & 4 deletions src/API.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using FluentValidation;
using Fossa.API.Core;
using Fossa.API.Core.Validators;
using Fossa.API.Infrastructure;
using Fossa.API.Persistence;
using Fossa.API.Web;
Expand Down Expand Up @@ -51,17 +53,22 @@

builder.Services.AddFrameworkPlatform();

builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblies(Seq(
var assemblies = Seq(
typeof(DefaultCoreModule),
typeof(DefaultInfrastructureModule),
typeof(DefaultPersistenceModule),
typeof(DefaultWebModule))
.Map(x => x.Assembly)
.ToArray());
.ToArray();

builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblies(assemblies);
cfg.AddOpenBehavior(typeof(ValidationBehavior<,>));
});

builder.Services.AddValidatorsFromAssemblies(assemblies);

builder.Services.Scan(scan => scan
.FromApplicationDependencies()
.AddClasses(classes => classes.AssignableTo(typeof(IMapper<,>)))
Expand Down