You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Previously we were using FluentValidation.AspNetCore to automatically validate parameter for controller actions. We then had an IAsyncActionFilter that was registered to detect if there are ModelState errors, and convert them into a custom error model. Since FluentValidation.AspNetCore is no longer supported we moved to SharpGrip.FluentValidation.AutoValidation however we noticed that SharpGrip seems to pre-empt our filter if there are errors and just return a plain ProblemDetails object.
After looking at the documentation I see you provide an AfterValidation interceptor, so I tried to convert our filter to be a IGlobalValidationInterceptor instead but I don't see how you get the list of failures in this method. The IValidationContext doesn't seem to have a way to get at the failures so I am wondering how is this used? Is the purpose of this method not to be able to take action based on the results of the validation? It's kind of hard to do that if I don't have access to the results of the validation in this method. Any help understanding would be greatly appreciated.
The text was updated successfully, but these errors were encountered:
You are correct that the validation interceptors are not receiving the validation result. This was an oversight on my part when implementing the validation interceptors.
Since adding a new parameter to the interface would break existing code, I will set the milestone for this to v2. I haven’t conducted a deep dive into the code to determine if there is a backward-compatible fix that avoids introducing new interfaces. However, if you discover anything, please let me know!
I was able to replace an ActionFilterAttribute, which converts ModelState errors to ValidationProblemDetails, and use a custom IFluentValidationAutoValidationResultFactory instead.
services.AddFluentValidationAutoValidation(configuration =>
{
// Replace the default result factory with a custom implementation.
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});
public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
{
// ModelState errors are available in context.ModelState
return new BadRequestObjectResult(validationProblemDetails);
}
}
Previously we were using FluentValidation.AspNetCore to automatically validate parameter for controller actions. We then had an IAsyncActionFilter that was registered to detect if there are ModelState errors, and convert them into a custom error model. Since FluentValidation.AspNetCore is no longer supported we moved to SharpGrip.FluentValidation.AutoValidation however we noticed that SharpGrip seems to pre-empt our filter if there are errors and just return a plain ProblemDetails object.
After looking at the documentation I see you provide an AfterValidation interceptor, so I tried to convert our filter to be a IGlobalValidationInterceptor instead but I don't see how you get the list of failures in this method. The IValidationContext doesn't seem to have a way to get at the failures so I am wondering how is this used? Is the purpose of this method not to be able to take action based on the results of the validation? It's kind of hard to do that if I don't have access to the results of the validation in this method. Any help understanding would be greatly appreciated.
The text was updated successfully, but these errors were encountered: