JasperFx / wolverine

Supercharged .NET server side development!
https://wolverinefx.net
MIT License
1.25k stars 137 forks source link

Allow customizing fluent validation response type with Wolverine endpoints #640

Open rigtigeEmil opened 11 months ago

rigtigeEmil commented 11 months ago

Is your feature request related to a problem? Please describe. I'm working on introducing Wolverine endpoints in an existing solution. This solution uses a model like this to describe any problems and return them to the caller:

public record ErrorResponse(IEnumerable<string>? Errors, string? ErrorCode, string? Message);

Opting into Wolverine's UseFluentValidationProblemDetailMiddleware() causes automatic validation of abstract validators, and while the response above this looks similar to the ProblemDetails that Wolverine (and .NET in general) uses, it causes obvious issues since the response signature for 4XX calls are different.

Describe the solution you'd like I'm assuming that somewhere Wolverine calls ValidationResult result = SomeValidator.Validate(xx). It'd be nice to be able to define what should happen in this case, and what should be returned in the endpoint. Alternatively, a solution could be to opt into having a falsy validation result throw an exception that could then be caught in some middleware and mapped to any type.

Describe alternatives you've considered Currently opting out of UseFluentValidationProblemDetailMiddleware() and manually creating an instance of a given validator, and if the result is invalid, returning 400.

jeremydmiller commented 11 months ago

@rigtigeEmil As we discussed on Discord, this is "I take pull requests" territory if you're interested in taking this work on.

rigtigeEmil commented 11 months ago

@jeremydmiller I'm up for a challenge, but to be honest I don't have the slightest idea of where to start with it

jeremydmiller commented 11 months ago

The Wolverine.Http.FluentValidation package isn't very much code. The Wolverine.FluentValidation package does have a mechanism to swap out how it handles the failures. In your case, I think you make a pluggable strategy about how to go from the ValidationResults to an AspNetCore IResult. There'll be a little more work to modify the OpenAPI metadata on the HttpChain model