FluentValidation.AutoValidation
FluentValidation.AutoValidation copied to clipboard
Improve/extend context in the IFluentValidationAutoValidationResultFactory
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>();
});
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.
Hi, thank you for the response i'll wait for v2 versione
Hi, I just reopened this issue so I can keep track of features to be built.