Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting validation failures in AfterValidation interceptor method #45

Open
nkamenar opened this issue Dec 10, 2024 · 2 comments
Open

Getting validation failures in AfterValidation interceptor method #45

nkamenar opened this issue Dec 10, 2024 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@nkamenar
Copy link

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.

@mvdgun
Copy link
Member

mvdgun commented Dec 10, 2024

Hi @nkamenar,

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!

See also: #12

@mvdgun mvdgun added the enhancement New feature or request label Dec 10, 2024
@mvdgun mvdgun added this to the v2.0 milestone Dec 10, 2024
@icnocop
Copy link
Contributor

icnocop commented Dec 12, 2024

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);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants