mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-05-05 12:22:35 +02:00
Move comments regex in helper file
This commit is contained in:
parent
58020420c3
commit
783e2c044d
3 changed files with 11 additions and 13 deletions
|
@ -1080,9 +1080,9 @@ components:
|
||||||
comment:
|
comment:
|
||||||
$ref: "#/components/schemas/comment"
|
$ref: "#/components/schemas/comment"
|
||||||
comment:
|
comment:
|
||||||
minLength: 5
|
minLength: 1
|
||||||
maxLength: 255
|
maxLength: 255
|
||||||
pattern: ".*" #everything except newlines ^[*]{5, 255}$
|
pattern: "^(.){1,255}$" # everything except newlines
|
||||||
type: string
|
type: string
|
||||||
example: "What a lovely picture! 😊"
|
example: "What a lovely picture! 😊"
|
||||||
description: The comment's text
|
description: The comment's text
|
||||||
|
@ -1092,7 +1092,7 @@ components:
|
||||||
format: binary
|
format: binary
|
||||||
minLength: 1
|
minLength: 1
|
||||||
maxLength: 10485760 # 10 MB
|
maxLength: 10485760 # 10 MB
|
||||||
pattern: "((.|\n)*)" # todo: review. Btw this means "any string"
|
pattern: "((.|\n)*)" # this accepts everything
|
||||||
|
|
||||||
generic_response:
|
generic_response:
|
||||||
type: object
|
type: object
|
||||||
|
|
|
@ -3,7 +3,6 @@ package api
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"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)
|
// 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 !helpers.MatchCommentOrBadRequest(request_body.Comment, w, rt.baseLogger) {
|
||||||
|
|
||||||
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)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,3 +31,10 @@ func MatchUsernameOrBadRequest(username string, w http.ResponseWriter, l logrus.
|
||||||
w,
|
w,
|
||||||
l)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue