Skip to content

Commit

Permalink
The FluentValidation.AspNetCore package is no longer being maintained…
Browse files Browse the repository at this point in the history
… and is now unsupported - move to the core FluentValidation package
  • Loading branch information
support committed Aug 20, 2023
1 parent 8ccf099 commit 2a14047
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Build\Grand.Common.props" />
<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
<PackageReference Include="GoogleAuthenticator" Version="3.1.0" />
<PackageReference Include="MassTransit" Version="8.0.16" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.16" />
Expand All @@ -18,7 +19,7 @@
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="FluentValidation" Version="11.7.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Grand.Domain\Grand.Domain.csproj" />
Expand Down
17 changes: 9 additions & 8 deletions src/Core/Grand.Infrastructure/StartupBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using FluentValidation;
using Grand.Domain.Data;
using Grand.Infrastructure.Caching.RabbitMq;
using Grand.Infrastructure.Configuration;
Expand All @@ -12,7 +13,6 @@
using Grand.SharedKernel;
using Grand.SharedKernel.Extensions;
using MassTransit;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -197,7 +197,8 @@ private static void AddMediator(this IServiceCollection services, ITypeSearcher
/// </summary>
/// <param name="services">Collection of service descriptors</param>
/// <param name="configuration">Configuration</param>
private static IMvcCoreBuilder RegisterApplication(IServiceCollection services, IConfiguration configuration)
/// <param name="typeSearcher">Type searcher</param>
private static IMvcCoreBuilder RegisterApplication(IServiceCollection services, IConfiguration configuration, ITypeSearcher typeSearcher)
{
//add accessor to HttpContext
services.AddHttpContextAccessor();
Expand Down Expand Up @@ -233,7 +234,7 @@ private static IMvcCoreBuilder RegisterApplication(IServiceCollection services,

CommonHelper.IgnoreAcl = performanceConfig.IgnoreAcl;
CommonHelper.IgnoreStoreLimitations = performanceConfig.IgnoreStoreLimitations;

services.AddTransient<FluentValidationFilter>();
var mvcCoreBuilder = services.AddMvcCore(options =>
{
Expand Down Expand Up @@ -261,16 +262,16 @@ private static IMvcCoreBuilder RegisterApplication(IServiceCollection services,
/// <param name="configuration">Configuration root of the application</param>
public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
//find startup configurations provided by other assemblies
var typeSearcher = new TypeSearcher();
services.AddSingleton<ITypeSearcher>(typeSearcher);

//register application
var mvcBuilder = RegisterApplication(services, configuration);
var mvcBuilder = RegisterApplication(services, configuration, typeSearcher);

//register extensions
RegisterExtensions(mvcBuilder, configuration);

//find startup configurations provided by other assemblies
var typeSearcher = new TypeSearcher();
services.AddSingleton<ITypeSearcher>(typeSearcher);

var startupConfigurations = typeSearcher.ClassesOfType<IStartupApplication>();

//Register startup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentValidation;
using FluentValidation.AspNetCore;
using Grand.SharedKernel.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -49,7 +48,11 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
var result = await validator.ValidateAsync(contextValidator);
if (result.IsValid) continue;

result.AddToModelState(context.ModelState, "");
if (!result.IsValid) {
foreach (var error in result.Errors) {
context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
}
var hasJsonData = context.HttpContext.Request.ContentType?.Contains("application/json") ?? false;
if (!hasJsonData) continue;
context.Result = new BadRequestObjectResult(context.ModelState);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Azure.Identity;
using FluentValidation;
using FluentValidation.AspNetCore;
using Grand.Business.Core.Interfaces.Authentication;
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Security;
Expand Down Expand Up @@ -254,7 +253,6 @@ public static IMvcBuilder AddGrandMvc(this IServiceCollection services, IConfigu
}

//Add fluentValidation
services.AddFluentValidationClientsideAdapters();
var typeSearcher = new TypeSearcher();
var assemblies = typeSearcher.GetAssemblies();
services.AddValidatorsFromAssemblies(assemblies);
Expand All @@ -265,7 +263,7 @@ public static IMvcBuilder AddGrandMvc(this IServiceCollection services, IConfigu

//register controllers as services, it'll allow to override them
mvcBuilder.AddControllersAsServices();

return mvcBuilder;
}

Expand Down

0 comments on commit 2a14047

Please sign in to comment.