RestApiv2/pkg/utils/validator/password.go

57 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package validator
import (
"errors"
"strconv"
"unicode"
)
type password struct {
minlength int
maxlength int
upSymbol int
}
func NewValidator(min int, max int, up int) *password {
return &password{
minlength: min,
maxlength: max,
upSymbol: up,
}
}
func (p *password) CheckValid(password string) (bool, error) {
var Upper int
if len(password) < p.minlength {
return false, p.errorMinSymbol()
}
if len(password) > p.maxlength {
return false, p.errorMaxSymbol()
}
for _, char := range password {
if unicode.IsUpper(char) {
Upper++
break
}
}
if Upper < p.upSymbol {
return false, p.errorUpperSymbol()
}
return true, nil
}
func (p *password) errorMinSymbol() error {
return errors.New("Слижком короткий пароль, минимальное количество : " + strconv.Itoa(p.minlength))
}
func (p *password) errorMaxSymbol() error {
return errors.New("Превышен допустимый лимит символов, максимальное количество : " + strconv.Itoa(p.maxlength))
}
func (p *password) errorUpperSymbol() error {
return errors.New("В пароле должны быть символ верхнего регистра, количество : " + strconv.Itoa(p.upSymbol))
}