go get -u github.com/go-passwd/validator
import "github.com/go-passwd/validator"
passwordValidator := validator.New(validator.MinLength(5, nil), validator.MaxLength(10, nil))
err := passwordValidator.Validate(form.Password)
if err != nil {
panic(err)
}
You can pass to every validator functions customError
parameter witch will be returned on error instead of default error.
import "github.com/go-passwd/validator"
passwordValidator := validator.New(validator.MinLength(5, errors.New("too short")), validator.MaxLength(10, errors.New("too long")))
err := passwordValidator.Validate(form.Password)
if err != nil {
panic(err)
}
Check if password is a common password.
Common password list is based on list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/
passwordValidator := validator.New(validator.CommonPassword(nil))
Count occurrences of a chars and compares it with required value.
passwordValidator := validator.New(validator.ContainsAtLeast(5, "abcdefghijklmnopqrstuvwxyz", nil))
Check if password contains only selected chars.
passwordValidator := validator.New(validator.ContainsOnly("abcdefghijklmnopqrstuvwxyz", nil))
Check if password length is not greater that defined length.
passwordValidator := validator.New(validator.MaxLength(10, nil))
Check if password length is not lower that defined length.
passwordValidator := validator.New(validator.MinLength(5, nil))
Always return custom error.
passwordValidator := validator.New(validator.Noop(nil))
Check if password match regexp pattern.
passwordValidator := validator.New(validator.Regex("^\\w+$", nil))
Check if password is sufficiently different from the attributes.
Attributes can be: user login, email, first name, last name, …
passwordValidator := validator.New(validator.Similarity([]string{"username", "[email protected]"}, nil, nil))
Check if password starts with one of letter.
passwordValidator := validator.New(validator.StartsWith("abcdefghijklmnopqrstuvwxyz", nil))
Check if password contains only unique chars.
passwordValidator := validator.New(validator.Unique(nil))