mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-03-14 06:06:15 +01:00
Add Regex matching
This commit is contained in:
parent
758be58b31
commit
df5b3fdda8
3 changed files with 29 additions and 2 deletions
|
@ -1064,8 +1064,8 @@ components:
|
||||||
$ref: "#/components/schemas/comment"
|
$ref: "#/components/schemas/comment"
|
||||||
comment:
|
comment:
|
||||||
minLength: 5
|
minLength: 5
|
||||||
maxLength: 100 #todo think about it
|
maxLength: 255
|
||||||
pattern: ".*" #everything except newlines
|
pattern: ".*" #everything except newlines ^[*]{5, 255}$
|
||||||
type: string
|
type: string
|
||||||
example: "What a lovely picture! 😊"
|
example: "What a lovely picture! 😊"
|
||||||
description: The comment's text
|
description: The comment's text
|
||||||
|
|
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -87,6 +88,19 @@ func (rt *_router) PostComment(w http.ResponseWriter, r *http.Request, ps httpro
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the comment is valid (should not contain newlines and at be between 5 and 255 characters)
|
||||||
|
stat, err := regexp.Match(`^[*]{5, 255}$`, []byte(request_body.Comment))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
helpers.SendInternalError(err, "Error matching regex", w, rt.baseLogger)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !stat {
|
||||||
|
helpers.SendBadRequest(w, "Invalid comment", rt.baseLogger)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// add the comment to the database
|
// add the comment to the database
|
||||||
success, err := rt.db.PostComment(uid, photo_id, request_body.UID, request_body.Comment)
|
success, err := rt.db.PostComment(uid, photo_id, request_body.UID, request_body.Comment)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
|
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
|
||||||
|
@ -22,6 +23,18 @@ func (rt *_router) UpdateUsername(w http.ResponseWriter, r *http.Request, ps htt
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stat, err := regexp.Match(`^[a-zA-Z0-9_]{3,16}$`, []byte(req.Name))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
helpers.SendInternalError(err, "Error while matching username", w, rt.baseLogger)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !stat { //todo: sta regex non me piace
|
||||||
|
helpers.SendBadRequest(w, "Username must be between 3 and 16 characters long and can only contain letters, numbers and underscores", rt.baseLogger)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
status, err := rt.db.UpdateUsername(uid, req.Name)
|
status, err := rt.db.UpdateUsername(uid, req.Name)
|
||||||
|
|
||||||
if status == database.ERR_EXISTS {
|
if status == database.ERR_EXISTS {
|
||||||
|
|
Loading…
Reference in a new issue