FluentValidation.AutoValidation icon indicating copy to clipboard operation
FluentValidation.AutoValidation copied to clipboard

Improve/extend context in the IFluentValidationAutoValidationResultFactory

Open wzaieddev opened this issue 2 years ago • 3 comments

hi, i'm using rules with ErrorCode and ErrorMessage, example:

RuleFor(request => request.FirstName).NotNull()
  .WithErrorCode(errorCode)
  .WithMessage(errorMessage);

i would like that my validation has ErrorCode and errorMessage

I'm using CustomResultFactory, find below my configuration, but i'm not able to find the object that contains errors with code and messagge (the same object that i find when i use manual validation)

FluentValidation.Results.ValidationFailure

public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
    public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
    {
       return new BadRequestObjectResult(new { Title = "Validation errors", ValidationErrors = validationProblemDetails?.Errors });
    }
}

services.AddFluentValidationAutoValidation(configuration =>
{
    // Disable the built-in .NET model (data annotations) validation.
    configuration.DisableBuiltInModelValidation = true;
     
    // Only validate controllers decorated with the `FluentValidationAutoValidation` attribute.
    configuration.ValidationStrategy = ValidationStrategy.All;

    // Enable validation for parameters bound from the `BindingSource.Body` binding source.
    configuration.EnableBodyBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from the `BindingSource.Form` binding source.
    configuration.EnableFormBindingSourceAutomaticValidation = true;

    // Enable validation for parameters bound from the `BindingSource.Query` binding source.
    configuration.EnableQueryBindingSourceAutomaticValidation = true;

    // Replace the default result factory with a custom implementation.
    configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});

wzaieddev avatar Sep 29 '23 09:09 wzaieddev

Hi, currently the complete validation result is not passed to the custom factory unfortunately. I will look into a way to expose it without introducing breaking changes. If not possible this feature is going to have to wait for v2.

mvdgun avatar Oct 02 '23 00:10 mvdgun

Hi, thank you for the response i'll wait for v2 versione

wzaieddev avatar Oct 02 '23 16:10 wzaieddev

Hi, I just reopened this issue so I can keep track of features to be built.

mvdgun avatar Oct 02 '23 20:10 mvdgun