validator: github.com/go-passwd/validator Index | Examples | Files

package validator

import "github.com/go-passwd/validator"

Package validator is a set of functions to validate passwords

Index

Examples

Package Files

common_password.go contains_at_least.go contains_only.go doc.go max_length.go min_length.go noop.go regex.go similarity.go starts_with.go unique.go validate_func.go validator.go

type ValidateFunc Uses

type ValidateFunc func(password string) error

ValidateFunc defines a function to validate

func CommonPassword Uses

func CommonPassword(customError error) ValidateFunc

CommonPassword returns ValidateFunc that validate whether the password is a common password.

The password is rejected if it occurs in a provided list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/

Code:

passwordValidator := CommonPassword(nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("qaz123"))
fmt.Println(passwordValidator("pa$$w0rd@"))

Output:

Password can't be a commonly used password
Password can't be a commonly used password
<nil>

Code:

err := errors.New("custom error message")
passwordValidator := CommonPassword(err)
fmt.Println(passwordValidator("password"))

Output:

custom error message

func ContainsAtLeast Uses

func ContainsAtLeast(chars string, occurrences int, customError error) ValidateFunc

ContainsAtLeast returns a ValidateFunc that count occurrences of a chars and compares it with required value

Code:

passwordValidator := ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("PASSWORD"))
fmt.Println(passwordValidator("passWORD"))

Output:

<nil>
Password must contains at least 4 chars from abcdefghijklmnopqrstuvwxyz
<nil>

Code:

err := errors.New("custom error message")
passwordValidator := ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, err)
fmt.Println(passwordValidator("PASSWORD"))

Output:

custom error message

func ContainsOnly Uses

func ContainsOnly(chars string, customError error) ValidateFunc

ContainsOnly returns a ValidateFunc that check if password contains only selected chars

Code:

passwordValidator := ContainsOnly("abcdefghijklmnopqrstuvwxyz", nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("password0"))
fmt.Println(passwordValidator("passWORD"))

Output:

<nil>
the password must contains only abcdefghijklmnopqrstuvwxyz
the password must contains only abcdefghijklmnopqrstuvwxyz

Code:

err := errors.New("custom error message")
passwordValidator := ContainsOnly("abcdefghijklmnopqrstuvwxyz", err)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("password0"))
fmt.Println(passwordValidator("passWORD"))

Output:

<nil>
custom error message
custom error message

func MaxLength Uses

func MaxLength(length int, customError error) ValidateFunc

MaxLength returns a ValidateFunc that check if password length is not greater that "length"

Code:

passwordValidator := MaxLength(5, nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("pass"))
fmt.Println(passwordValidator("passw"))

Output:

Password length must be not greater that 5 chars
<nil>
<nil>

Code:

err := errors.New("custom error message")
passwordValidator := MaxLength(5, err)
fmt.Println(passwordValidator("password"))

Output:

custom error message

func MinLength Uses

func MinLength(length int, customError error) ValidateFunc

MinLength returns a ValidateFunc that check if password length is not lower that "length"

Code:

passwordValidator := MinLength(5, nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("pass"))
fmt.Println(passwordValidator("passw"))

Output:

<nil>
Password length must be not lower that 5 chars
<nil>

Code:

err := errors.New("custom error message")
passwordValidator := MinLength(5, err)
fmt.Println(passwordValidator("pass"))

Output:

custom error message

func Noop Uses

func Noop(customError error) ValidateFunc

Noop returns a ValidateFunc that always return custom error

Code:

passwordValidator := Noop(nil)
fmt.Println(passwordValidator("password"))

Output:

<nil>

Code:

err := errors.New("custom error message")
passwordValidator := Noop(err)
fmt.Println(passwordValidator("password"))

Output:

custom error message

func Regex Uses

func Regex(pattern string, customError error) ValidateFunc

Regex returns ValidateFunc that check if password match regexp pattern

Code:

passwordValidator := Regex("^\\w+$", nil)
fmt.Println(passwordValidator("password"))
fmt.Println(passwordValidator("pa$$w0rd"))

Output:

Password shouldn't match "^\w+$" pattern
<nil>

Code:

err := errors.New("custom error message")
passwordValidator := Regex("^\\w+$", err)
fmt.Println(passwordValidator("password"))

Output:

custom error message

func Similarity Uses

func Similarity(attributes []string, maxSimilarity *float64, customError error) ValidateFunc

Similarity returns ValidateFunc that validate whether the password is sufficiently different from the attributes

Attributes can be: user login, email, first name, last name, …

Code:

passwordValidator := Similarity([]string{"username", "username@example.com"}, nil, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("example"))
similarity := 0.5
passwordValidator = Similarity([]string{"username", "username@example.com"}, &similarity, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("examplecom"))

Output:

The password is too similar to the username
<nil>
The password is too similar to the username
The password is too similar to the username@example.com

Code:

err := errors.New("custom error message")
passwordValidator := Similarity([]string{"username", "username@example.com"}, nil, err)
fmt.Println(passwordValidator("username"))

Output:

custom error message

func StartsWith Uses

func StartsWith(letters string, customError error) ValidateFunc

StartsWith returns ValidateFunc that validate whether the password is starts with one of letter

Code:

passwordValidator := StartsWith("abc", nil)
fmt.Println(passwordValidator("bui87j"))
fmt.Println(passwordValidator("qwerty"))

Output:

<nil>
the password must starts with one of: abc

Code:

err := errors.New("custom error message")
passwordValidator := StartsWith("abc", err)
fmt.Println(passwordValidator("bui87j"))
fmt.Println(passwordValidator("qwerty"))

Output:

<nil>
custom error message

func Unique Uses

func Unique(customError error) ValidateFunc

Unique returns ValidateFunc that validate whether the password has only unique chars

Code:

passwordValidator := Unique(nil)
fmt.Println(passwordValidator("bui87j"))
fmt.Println(passwordValidator("qwerte"))

Output:

<nil>
the password must contains unique chars

Code:

err := errors.New("custom error message")
passwordValidator := Unique(err)
fmt.Println(passwordValidator("bui87j"))
fmt.Println(passwordValidator("qwerte"))

Output:

<nil>
custom error message

type Validator Uses

type Validator []ValidateFunc

Validator represents set of password validators

func New Uses

func New(vfunc ...ValidateFunc) *Validator

New return new instance of Validator

Code:

New(MinLength(5, nil), MaxLength(10, nil))

func (*Validator) Validate Uses

func (v *Validator) Validate(password string) error

Validate the password

Code:

passwordValidator := Validator{MinLength(5, nil), MaxLength(10, nil)}
fmt.Println(passwordValidator.Validate("password"))
fmt.Println(passwordValidator.Validate("pass1"))
fmt.Println(passwordValidator.Validate("password12"))
fmt.Println(passwordValidator.Validate("pass"))
fmt.Println(passwordValidator.Validate("password123"))

Output:

<nil>
<nil>
<nil>
Password length must be not lower that 5 chars
Password length must be not greater that 10 chars

Package validator imports 4 packages (graph). Updated 2018-09-12. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).