feat(project): 🦺 improve validation

master
Xeronith 2023-07-12 09:55:07 +03:30
rodzic a107dcd600
commit 9df2bd01bd
6 zmienionych plików z 19 dodań i 30 usunięć

Wyświetl plik

@ -7,7 +7,7 @@ import (
)
func ResetPassword(x IDispatcher, usernameOrEmail string) (IResetPasswordResult, error) {
isEmail := x.MatchString(EMAIL, usernameOrEmail)
isEmail := REGEXP_EMAIL.MatchString(usernameOrEmail)
if !isEmail && !UsernameIsValid(usernameOrEmail) {
return nil, ERROR_INVALID_PARAMETERS
}

Wyświetl plik

@ -1,11 +1,7 @@
package validators
import (
"regexp"
. "github.com/reiver/greatape/components/constants"
)
import . "github.com/reiver/greatape/components/constants"
func EmailIsValid(input string) bool {
return regexp.MustCompile(EMAIL).MatchString(input)
return REGEXP_EMAIL.MatchString(input)
}

Wyświetl plik

@ -1,13 +1,9 @@
package validators
import (
"regexp"
. "github.com/reiver/greatape/components/constants"
)
import . "github.com/reiver/greatape/components/constants"
func UsernameIsValid(username string) bool {
if !regexp.MustCompile(USERNAME).MatchString(username) {
if !REGEXP_USERNAME.MatchString(username) {
return false
}

Wyświetl plik

@ -1,11 +1,7 @@
package validators
import (
"regexp"
. "github.com/reiver/greatape/components/constants"
)
import . "github.com/reiver/greatape/components/constants"
func WebfingerIsValid(webfinger string) bool {
return regexp.MustCompile(WEBFINGER).MatchString(webfinger)
return REGEXP_WEBFINGER.MatchString(webfinger)
}

Wyświetl plik

@ -1,11 +0,0 @@
package constants
// noinspection GoSnakeCaseUsage, GoUnusedConst
const (
USERNAME = "^[a-z0-9_\\.]{5,16}$"
PASSWORD = "^.{6,}$"
PHONE_NUMBER = "^9\\d{9}$"
URL = "^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$"
EMAIL = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
WEBFINGER = "^acct:[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
)

Wyświetl plik

@ -0,0 +1,12 @@
package constants
import "regexp"
var (
REGEXP_USERNAME = regexp.MustCompile(`^[a-z0-9_\.]{5,16}$`)
REGEXP_PASSWORD = regexp.MustCompile(`^.{6,}$`)
REGEXP_PHONE_NUMBER = regexp.MustCompile(`^9\d{9}$`)
REGEXP_URL = regexp.MustCompile(`^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|\/|\/\/)?[A-z0-9_-]*?[:]?[A-z0-9_-]*?[@]?[A-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$`)
REGEXP_EMAIL = regexp.MustCompile(`^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$`)
REGEXP_WEBFINGER = regexp.MustCompile(`^acct:[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$`)
)