Move comments regex in helper file

This commit is contained in:
Marco Realacci 2022-12-22 18:08:03 +01:00
parent 58020420c3
commit 783e2c044d
3 changed files with 11 additions and 13 deletions

View file

@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"net/http"
"regexp"
"strconv"
"github.com/julienschmidt/httprouter"
@ -89,15 +88,7 @@ func (rt *_router) PostComment(w http.ResponseWriter, r *http.Request, ps httpro
}
// check if the comment is valid (should not contain newlines and at be between 5 and 255 characters)
stat, err := regexp.Match(`^(.*)*`, []byte(request_body.Comment))
if err != nil {
helpers.SendInternalError(err, "Error matching regex", w, rt.baseLogger)
return
}
if !stat || len(request_body.Comment) < 5 || len(request_body.Comment) > 255 {
helpers.SendBadRequest(w, "Invalid comment", rt.baseLogger)
if !helpers.MatchCommentOrBadRequest(request_body.Comment, w, rt.baseLogger) {
return
}

View file

@ -31,3 +31,10 @@ func MatchUsernameOrBadRequest(username string, w http.ResponseWriter, l logrus.
w,
l)
}
func MatchCommentOrBadRequest(comment string, w http.ResponseWriter, l logrus.FieldLogger) bool {
return MatchRegexOrBadRequest(comment,
`^(.){1,255}$`, "Comment must be between 1 and 255 characters long",
w,
l)
}