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

Improve/extend context in the IFluentValidationAutoValidationResultFactory #12

Open
wzaieddev opened this issue Sep 29, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@wzaieddev
Copy link

wzaieddev commented Sep 29, 2023

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>();
});
@mvdgun mvdgun self-assigned this Oct 1, 2023
@mvdgun mvdgun added the enhancement New feature or request label Oct 1, 2023
@mvdgun
Copy link
Member

mvdgun commented Oct 2, 2023

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.

@wzaieddev
Copy link
Author

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

@mvdgun mvdgun reopened this Oct 2, 2023
@mvdgun
Copy link
Member

mvdgun commented Oct 2, 2023

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

@mvdgun mvdgun added this to the v2.0 milestone Oct 15, 2023
@mvdgun mvdgun changed the title NET 6 - FluentValidation AutoValidation - Improve/extend context in the IFluentValidationAutoValidationResultFactory Oct 19, 2023
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

2 participants