mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-05-05 12:22:35 +02:00
Move username regex check in a helper function
This commit is contained in:
parent
1d11a5ba81
commit
58020420c3
4 changed files with 58 additions and 27 deletions
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
DEFAULT_LIMIT = 15 // don't know if should be moved to config
|
||||
DEFAULT_LIMIT = 15
|
||||
DEFAULT_OFFSET = 0
|
||||
)
|
||||
|
||||
|
|
33
service/api/helpers/regex-helpers.go
Normal file
33
service/api/helpers/regex-helpers.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func MatchRegexOrBadRequest(str string, regex string, error_description string, w http.ResponseWriter, l logrus.FieldLogger) bool {
|
||||
|
||||
stat, err := regexp.Match(regex, []byte(str))
|
||||
|
||||
if err != nil {
|
||||
SendInternalError(err, "Error while matching username regex", w, l)
|
||||
return false
|
||||
}
|
||||
|
||||
if !stat {
|
||||
// string didn't match the regex, so it's invalid, let's send a bad request error
|
||||
SendBadRequest(w, error_description, l)
|
||||
return false
|
||||
}
|
||||
// string matched the regex, so it's valid
|
||||
return true
|
||||
}
|
||||
|
||||
func MatchUsernameOrBadRequest(username string, w http.ResponseWriter, l logrus.FieldLogger) bool {
|
||||
return MatchRegexOrBadRequest(username,
|
||||
`^[a-zA-Z0-9_]{3,16}$`, "Username must be between 3 and 16 characters long and can only contain letters, numbers and underscores",
|
||||
w,
|
||||
l)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue