mirror of
https://gitee.com/goploy/goploy.git
synced 2024-11-30 11:17:50 +08:00
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package validator
|
|
|
|
import (
|
|
ut "github.com/go-playground/universal-translator"
|
|
"gopkg.in/go-playground/validator.v9"
|
|
"unicode"
|
|
)
|
|
|
|
func registerPassword() {
|
|
_ = Validate.RegisterValidation("password", func(fl validator.FieldLevel) bool {
|
|
password := fl.Field().String()
|
|
if len(password) < 8 || len(password) > 16 {
|
|
return false
|
|
}
|
|
var (
|
|
hasLetter = false
|
|
hasNumber = false
|
|
hasSpecial = false
|
|
)
|
|
|
|
for _, char := range password {
|
|
switch {
|
|
case unicode.IsLetter(char):
|
|
hasLetter = true
|
|
case unicode.IsNumber(char):
|
|
hasNumber = true
|
|
case unicode.IsPunct(char) || unicode.IsSymbol(char):
|
|
hasSpecial = true
|
|
}
|
|
}
|
|
|
|
if hasLetter && hasNumber {
|
|
return true
|
|
} else if hasLetter && hasSpecial {
|
|
return true
|
|
} else if hasNumber && hasSpecial {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
})
|
|
|
|
_ = Validate.RegisterTranslation("password", Trans, func(ut ut.Translator) error {
|
|
return ut.Add("password", "{0} policy is min:8, max:16 and at least one alpha and at least one special char!", true) // see universal-translator for details
|
|
}, func(ut ut.Translator, fe validator.FieldError) string {
|
|
t, _ := ut.T("password", fe.Field())
|
|
|
|
return t
|
|
})
|
|
}
|