Skip to content

Commit

Permalink
feat: expose QueryParam on api package
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane committed Aug 18, 2024
1 parent f4ef8dd commit f631e37
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f631e37

Please sign in to comment.