Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

FluentValidation - Upgrade to V11 (#288)

Krzysztof Pajak3e2746ebe3a6
diff --git a/src/API/Grand.Api/Controllers/TokenController.cs b/src/API/Grand.Api/Controllers/TokenController.cs
index 79c9d5616..7158b469b 100644
--- a/src/API/Grand.Api/Controllers/TokenController.cs
+++ b/src/API/Grand.Api/Controllers/TokenController.cs
@@ -29,6 +29,9 @@ namespace Grand.Api.Controllers
[HttpPost]
public async Task<IActionResult> Create([FromBody] LoginModel model)
{
+ if (!ModelState.IsValid)
+ return BadRequest(ModelState);
+
var claims = new Dictionary<string, string> {
{ "Email", model.Email }
};
diff --git a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
index 65d6ee516..ce789c5d1 100644
--- a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
+++ b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
@@ -18,7 +18,7 @@
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.6.48" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
- <PackageReference Include="FluentValidation.AspNetCore" Version="10.4.0" />
+ <PackageReference Include="FluentValidation.AspNetCore" Version="11.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Grand.Domain\Grand.Domain.csproj" />
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index 3ee9545a7..4c562c546 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -7,6 +7,7 @@ using Grand.Infrastructure.Plugins;
using Grand.Infrastructure.Roslyn;
using Grand.Infrastructure.TypeConverters;
using Grand.Infrastructure.TypeSearchers;
+using Grand.Infrastructure.Validators;
using Grand.SharedKernel;
using Grand.SharedKernel.Extensions;
using MassTransit;
@@ -209,7 +210,11 @@ namespace Grand.Infrastructure
CommonHelper.IgnoreAcl = performanceConfig.IgnoreAcl;
CommonHelper.IgnoreStoreLimitations = performanceConfig.IgnoreStoreLimitations;
- var mvcCoreBuilder = services.AddMvcCore();
+ services.AddTransient<FluentValidationFilter>();
+ var mvcCoreBuilder = services.AddMvcCore(options =>
+ {
+ options.Filters.AddService<FluentValidationFilter>();
+ });
return mvcCoreBuilder;
}
diff --git a/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs b/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
new file mode 100644
index 000000000..9a45f8bbf
--- /dev/null
+++ b/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
@@ -0,0 +1,57 @@
+using FluentValidation;
+using FluentValidation.AspNetCore;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace Grand.Infrastructure.Validators
+{
+ public class FluentValidationFilter : IAsyncActionFilter
+ {
+ #region Fields
+
+ private readonly IServiceProvider _serviceProvider;
+
+ #endregion
+
+ #region Ctor
+
+ public FluentValidationFilter(IServiceProvider serviceProvider)
+ {
+ _serviceProvider = serviceProvider;
+ }
+
+ #endregion
+
+ #region Methods
+
+ /// <summary>
+ /// Called before the action executes, after model binding is complete
+ /// </summary>
+ /// <param name="context">A context for action filters</param>
+ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
+ {
+ //only in POST requests
+ if (!HttpMethods.IsPost(context.HttpContext.Request.Method))
+ {
+ await next();
+ return;
+ }
+ if (context.ActionArguments.TryGetValue("model", out var model))
+ {
+ Type genericType = typeof(IValidator<>).MakeGenericType(model.GetType());
+ if (genericType is not null)
+ {
+ var validator = (IValidator)_serviceProvider.GetService(genericType);
+ if (validator is not null)
+ {
+ var contextvalidator = new ValidationContext<object>(model);
+ var result = await validator.ValidateAsync(contextvalidator);
+ if (!result.IsValid) result.AddToModelState(context.ModelState, "");
+ }
+ }
+ }
+ await next();
+ }
+ #endregion
+ }
+}
diff --git a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
index 9e64cf7ec..3d19ec1f9 100644
--- a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
+++ b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
@@ -241,9 +241,8 @@ namespace Grand.Web.Common.Infrastructure
var typeSearcher = new AppTypeSearcher();
var assemblies = typeSearcher.GetAssemblies();
configuration.RegisterValidatorsFromAssemblies(assemblies);
- configuration.DisableDataAnnotationsValidation = true;
- //implicit/automatic validation of child properties
- configuration.ImplicitlyValidateChildProperties = true;
+ configuration.DisableDataAnnotationsValidation = true;
+ configuration.AutomaticValidationEnabled = false;
});
//MVC now serializes JSON with camel case names by default, use this code to avoid it