danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.71k stars 134 forks source link

Accessing validation results in the request pipeline #453

Open guneyizol opened 1 month ago

guneyizol commented 1 month ago

Hi,

Is there a way to access the validation results in the downstream middlewares and/or handlers in the request pipeline before returning a response? I am happy with the built-in validation but I want to access and process the successfully validated fields and return http.StatusOK if there are any. (If this approach is not really compliant with the HTTP semantics, please let me know.)

What I am ultimately trying is to validate an input json object with possibly many fields and process all of the valid ones and let the user know which ones are successfully processed and which ones are not.

Thank you very much 🙏

danielgtaylor commented 1 month ago

@guneyizol this isn't really possible using the built-in request validation pipeline, however I think you could use the SkipValidate* settings in huma.Operation to bypass the built-in validation and then do the validation yourself in the handler. A quick & easy way to try it out would be to use the huma.ModelValidator and if that works to your satisfaction you can look inside its implementation to see how you can make your handler more efficient (e.g. pre-allocating schemas, path buffers, result objects, using sync.Pool, etc if needed).

Hope that helps!