Removed unused file

This commit is contained in:
Lilleman auf Larv 2020-12-29 13:52:15 +01:00
parent 1811f200c0
commit 39e5c84cf6

View File

@ -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
}