add validation filters
This commit is contained in:
21
Installers/InstallFilters.cs
Normal file
21
Installers/InstallFilters.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using Diary.Shared;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Diary.Installers
|
||||||
|
{
|
||||||
|
public static class InstallFilters
|
||||||
|
{
|
||||||
|
public static void AddFilters(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services
|
||||||
|
.Configure<ApiBehaviorOptions>(opt =>
|
||||||
|
{
|
||||||
|
opt.SuppressModelStateInvalidFilter = true;
|
||||||
|
})
|
||||||
|
.AddControllers(opt =>
|
||||||
|
{
|
||||||
|
opt.Filters.Add<ValidationFilter>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Program.cs
10
Program.cs
@@ -10,17 +10,16 @@ using System.Reflection;
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Settings - Done by sefaulr
|
* Settings - Done
|
||||||
* Logging - Done
|
* Logging - Done
|
||||||
* Database - Donw
|
* Database - Done
|
||||||
* Dependencies
|
|
||||||
* Automapper - Done
|
* Automapper - Done
|
||||||
* Cors
|
* Cors
|
||||||
* Views/Filters/Validation
|
* Views
|
||||||
* Swagger - Done
|
* Swagger - Done
|
||||||
* Auth
|
* Auth
|
||||||
* Exception Middleware - Done
|
* Exception Middleware - Done
|
||||||
* Validation Filter
|
* Validation Filter - Done
|
||||||
* */
|
* */
|
||||||
|
|
||||||
//+Setup Logger
|
//+Setup Logger
|
||||||
@@ -59,6 +58,7 @@ builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
|||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
builder.Services.AddExceptionsMiddleware();
|
builder.Services.AddExceptionsMiddleware();
|
||||||
|
builder.Services.AddFilters();
|
||||||
|
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
|||||||
43
Shared/ValidationFilter.cs
Normal file
43
Shared/ValidationFilter.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
namespace Diary.Shared
|
||||||
|
{
|
||||||
|
public class ValidationFilter : IAsyncActionFilter
|
||||||
|
{
|
||||||
|
public const string OverviewTitle = "The request is invalid.";
|
||||||
|
public const string OverviewMessage = "The request sent data that is not correct for the request.";
|
||||||
|
|
||||||
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||||
|
{
|
||||||
|
if (context.ModelState.IsValid)
|
||||||
|
{
|
||||||
|
await next();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ValidationExceptionDetails errorObj = new()
|
||||||
|
{
|
||||||
|
Title = OverviewTitle,
|
||||||
|
Message = OverviewMessage,
|
||||||
|
ModelState = GetErrors(context.ModelState)
|
||||||
|
};
|
||||||
|
context.Result = new BadRequestObjectResult(errorObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ValidationProblemDescriptor> GetErrors(ModelStateDictionary modelState)
|
||||||
|
{
|
||||||
|
List<ValidationProblemDescriptor> errors = new();
|
||||||
|
|
||||||
|
return modelState.Where(ms => ms.Value?.Errors.Count > 0)
|
||||||
|
.Select(x => new ValidationProblemDescriptor
|
||||||
|
{
|
||||||
|
Property = x.Key,
|
||||||
|
Errors = x.Value?.Errors.Select(e => e.ErrorMessage).ToArray()
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user