From f631e379fbbfe4e0ae0b48cee0cf197ab334694e Mon Sep 17 00:00:00 2001 From: Olakunle Arewa Date: Sun, 18 Aug 2024 01:48:22 +0100 Subject: [PATCH] feat: expose QueryParam on api package --- api/request.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/api/request.go b/api/request.go index 6b00ce7..913aa0a 100644 --- a/api/request.go +++ b/api/request.go @@ -41,6 +41,33 @@ func ReadJSON(r *http.Request, v interface{}) { } } +// QueryParams converts the query values of the request into a struct using +// the "json" tag to map the keys. It supports transformations using modl +// and validation provided by ozzo. It panics with a 422 if the value fails +// ozzo validation and 400 due to any other error +func QueryParam(r *http.Request, v interface{}) { + err := requests.QueryParams(r, v) + if err == nil { + return + } + + var e validation.Errors + switch { + case errors.As(err, &e): + panic(Err{ + Code: http.StatusBadRequest, + Message: "We could not validate your request.", + Data: e, + }) + default: + panic(Err{ + Code: http.StatusBadRequest, + Message: "We cannot parse your request body.", + Err: err, + }) + } +} + // IDParam extracts a uint URL parameter from the given request. panics with a 400 if // the param is not a strin, otherwise it panics with a basic error. func IDParam(r *http.Request, name string) uint {