Skip to content

Improve/extend context in the IFluentValidationAutoValidationResultFactory #12

Open
@wzaieddev

Description

@wzaieddev

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions