mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
Fix/identity service model validation (#1837)
* Fix indentation * Fix comment * Extract ModelStateValidationFilter add to Indentity MVC opts * Remove unnecessary base constructor call
This commit is contained in:
@ -0,0 +1,33 @@
|
||||
using System.Linq;
|
||||
using Bit.Core.Models.Api;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Bit.SharedWeb.Utilities
|
||||
{
|
||||
public class ModelStateValidationFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
public ModelStateValidationFilterAttribute()
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
var model = context.ActionArguments.FirstOrDefault(a => a.Key == "model");
|
||||
if (model.Key == "model" && model.Value == null)
|
||||
{
|
||||
context.ModelState.AddModelError(string.Empty, "Body is empty.");
|
||||
}
|
||||
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
OnModelStateInvalid(context);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnModelStateInvalid(ActionExecutingContext context)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(new ErrorResponseModel(context.ModelState));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user