fix: refactor the idp and regex code (#2030)

* refactor: validate util and idp

* chore: clean code

* chore: clean code
This commit is contained in:
Yaodong Yu
2023-06-29 21:44:14 +08:00
committed by GitHub
parent 49a73f8138
commit 1c42b6e395
4 changed files with 40 additions and 23 deletions

View File

@ -22,10 +22,18 @@ import (
"github.com/nyaruka/phonenumbers"
)
var rePhone *regexp.Regexp
var (
rePhone *regexp.Regexp
ReWhiteSpace *regexp.Regexp
ReFieldWhiteList *regexp.Regexp
ReUserName *regexp.Regexp
)
func init() {
rePhone, _ = regexp.Compile(`(\d{3})\d*(\d{4})`)
ReWhiteSpace, _ = regexp.Compile(`\s`)
ReFieldWhiteList, _ = regexp.Compile(`^[A-Za-z0-9]+$`)
ReUserName, _ = regexp.Compile("^[a-zA-Z0-9]+((?:-[a-zA-Z0-9]+)|(?:_[a-zA-Z0-9]+))*$")
}
func IsEmailValid(email string) bool {
@ -70,3 +78,7 @@ func GetCountryCode(prefix string, phone string) (string, error) {
return countryCode, nil
}
func FilterField(field string) bool {
return ReFieldWhiteList.MatchString(field)
}