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.

Update validation filter

supporte23f8c5bc8d1
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index ff4542451..d3f793153 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -229,11 +229,10 @@ namespace Grand.Infrastructure
CommonPath.WebHostEnvironment = hostingEnvironment.WebRootPath;
CommonPath.BaseDirectory = hostingEnvironment.ContentRootPath;
-
- services.AddTransient<FluentValidationFilter>();
+ services.AddTransient<ValidationFilter>();
var mvcCoreBuilder = services.AddMvcCore(options =>
{
- options.Filters.AddService<FluentValidationFilter>();
+ options.Filters.AddService<ValidationFilter>();
var frontConfig = new FrontendAPIConfig();
configuration.GetSection("FrontendAPI").Bind(frontConfig);
if (frontConfig.JsonContentType)
diff --git a/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs b/src/Core/Grand.Infrastructure/Validators/ValidationFilter.cs
similarity index 85%
rename from src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
rename to src/Core/Grand.Infrastructure/Validators/ValidationFilter.cs
index 539c568a5..0b0681c30 100644
--- a/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
+++ b/src/Core/Grand.Infrastructure/Validators/ValidationFilter.cs
@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Mvc.Filters;
namespace Grand.Infrastructure.Validators
{
- public class FluentValidationFilter : IAsyncActionFilter
+ public class ValidationFilter : IAsyncActionFilter
{
#region Fields
@@ -16,7 +16,7 @@ namespace Grand.Infrastructure.Validators
#region Ctor
- public FluentValidationFilter(IServiceProvider serviceProvider)
+ public ValidationFilter(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
@@ -38,7 +38,7 @@ namespace Grand.Infrastructure.Validators
await next();
return;
}
-
+
foreach (var argument in context.ActionArguments.Where(x => !CommonHelper.IsSimpleType(x.Value?.GetType())))
{
Type targetType = argument.Value!.GetType();
@@ -51,6 +51,7 @@ namespace Grand.Infrastructure.Validators
{
continue;
}
+
var contextValidator = new ValidationContext<object>(argument.Value);
var result = await validator.ValidateAsync(contextValidator);
if (result.IsValid) continue;
@@ -59,17 +60,18 @@ namespace Grand.Infrastructure.Validators
context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
}
+ }
- var hasJsonData = context.HttpContext.Request.ContentType?.Contains("application/json") ?? false;
- if (!hasJsonData) continue;
- if (context.ModelState.IsValid) continue;
+ var hasJsonData = context.HttpContext.Request.ContentType?.Contains("application/json") ?? false;
+ if (hasJsonData && !context.ModelState.IsValid)
+ {
context.Result = new BadRequestObjectResult(context.ModelState);
return;
-
}
await next();
}
+
#endregion
}
}
\ No newline at end of file
diff --git a/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs b/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs
index 416a3b6b5..9ca61c19f 100644
--- a/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs
+++ b/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs
@@ -12,7 +12,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Grand.Infrastructure.Tests.Validators
{
[TestClass()]
- public class FluentValidationFilterTests
+ public class ValidationFilterTests
{
[TestMethod()]
public async Task OnActionExecutionAsyncTest_ValidModel()
@@ -23,7 +23,7 @@ namespace Grand.Infrastructure.Tests.Validators
services.AddScoped<IValidator<SourceTest>, SourceTestValidator>();
var serviceProvider = services.BuildServiceProvider();
- var actionFilter = new FluentValidationFilter(serviceProvider);
+ var actionFilter = new ValidationFilter(serviceProvider);
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "POST";
@@ -66,7 +66,7 @@ namespace Grand.Infrastructure.Tests.Validators
services.AddScoped<IValidator<SourceTest>, SourceTestValidator>();
var serviceProvider = services.BuildServiceProvider();
- var actionFilter = new FluentValidationFilter(serviceProvider);
+ var actionFilter = new ValidationFilter(serviceProvider);
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "POST";