From 39e5c84cf6718ef8bdf5a67233727dc32a962d07 Mon Sep 17 00:00:00 2001 From: Lilleman Date: Tue, 29 Dec 2020 13:52:15 +0100 Subject: [PATCH] Removed unused file --- src/utils.go | 62 ---------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 src/utils.go diff --git a/src/utils.go b/src/utils.go deleted file mode 100644 index 382bdc8..0000000 --- a/src/utils.go +++ /dev/null @@ -1,62 +0,0 @@ -package utils - -import ( - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "strings" -) - -type malformedRequest struct { - status int - msg string -} - -func (mr *malformedRequest) Error() string { - return mr.msg -} - -// ValidateJSONBody validates a JSON request body to a destionation interface -func ValidateJSONBody(req *http.Request, dst interface{}) error { - dec := json.NewDecoder(req.Body) - dec.DisallowUnknownFields() - - err := dec.Decode(&dst) - if err != nil { - var syntaxError *json.SyntaxError - var unmarshalTypeError *json.UnmarshalTypeError - - if errors.As(err, &syntaxError) { - msg := fmt.Sprintf("Request body contains badly-formed JSON (at position %d)", syntaxError.Offset) - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } else if errors.Is(err, io.ErrUnexpectedEOF) { - msg := fmt.Sprintf("Request body contains badly-formed JSON") - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } else if errors.As(err, &unmarshalTypeError) { - msg := fmt.Sprintf("Request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset) - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } else if strings.HasPrefix(err.Error(), "json: unknown field ") { - fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ") - msg := fmt.Sprintf("Request body contains unknown field %s", fieldName) - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } else if errors.Is(err, io.EOF) { - msg := "Request body must not be empty" - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } else if err.Error() == "http: request body too large" { - msg := "Request body must not be larger than 1MB" - return &malformedRequest{status: http.StatusRequestEntityTooLarge, msg: msg} - } else { - return err - } - } - - err = dec.Decode(&struct{}{}) - if err != io.EOF { - msg := "Request body must only contain a single JSON object" - return &malformedRequest{status: http.StatusBadRequest, msg: msg} - } - - return nil -}