Skip to content

Commit e2bc4c2

Browse files
committed
added custom error to validator functions
1 parent 82666c5 commit e2bc4c2

14 files changed

+119
-28
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ go get -u github.com/go-passwd/validator
1616
~~~go
1717
import "github.com/go-passwd/validator"
1818

19-
passwordValidator := validator.New(validator.MinLength(5), validator.MaxLength(10))
19+
passwordValidator := validator.New(validator.MinLength(5, nil), validator.MaxLength(10, nil))
20+
err := passwordValidator.Validate(form.Password)
21+
if err != nil {
22+
panic(err)
23+
}
24+
~~~
25+
26+
You can pass to every validator functions ``customError`` parameter witch will be returned on error instead of default error.
27+
28+
~~~go
29+
import "github.com/go-passwd/validator"
30+
31+
passwordValidator := validator.New(validator.MinLength(5, errors.New("too short")), validator.MaxLength(10, errors.New("too long")))
2032
err := passwordValidator.Validate(form.Password)
2133
if err != nil {
2234
panic(err)
@@ -30,23 +42,23 @@ if err != nil {
3042
Check if password length is not lower that defined length.
3143

3244
~~~go
33-
passwordValidator := validator.New(validator.MinLength(5))
45+
passwordValidator := validator.New(validator.MinLength(5, nil))
3446
~~~
3547

3648
### MaxLength
3749

3850
Check if password length is not greater that defined length.
3951

4052
~~~go
41-
passwordValidator := validator.New(validator.MaxLength(10))
53+
passwordValidator := validator.New(validator.MaxLength(10, nil))
4254
~~~
4355

4456
### ContainsAtLeast
4557

4658
Count occurrences of a chars and compares it with required value.
4759

4860
~~~go
49-
passwordValidator := validator.New(validator.ContainsAtLeast(5, "abcdefghijklmnopqrstuvwxyz"))
61+
passwordValidator := validator.New(validator.ContainsAtLeast(5, "abcdefghijklmnopqrstuvwxyz", nil))
5062
~~~
5163

5264
### CommonPassword
@@ -56,13 +68,13 @@ Check if password is a common password.
5668
Common password list is based on list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/
5769

5870
~~~go
59-
passwordValidator := validator.New(validator.CommonPassword())
71+
passwordValidator := validator.New(validator.CommonPassword(nil))
6072
~~~
6173

6274
### Regex
6375

6476
Check if password match regexp pattern.
6577

6678
~~~go
67-
passwordValidator := validator.New(validator.Regex("^\\w+$"))
79+
passwordValidator := validator.New(validator.Regex("^\\w+$", nil))
6880
~~~

common_password.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ var commonPasswords = []string{"123456", "123456789", "qwerty", "password", "111
1111
//
1212
// The password is rejected if it occurs in a provided list created by Mark Burnett:
1313
// https://xato.net/passwords/more-top-worst-passwords/
14-
func CommonPassword() ValidateFunc {
14+
func CommonPassword(customError error) ValidateFunc {
1515
return ValidateFunc(func(password string) error {
1616
password = strings.ToLower(password)
1717
for _, commonPassword := range commonPasswords {
1818
if commonPassword == password {
19+
if customError != nil {
20+
return customError
21+
}
1922
return fmt.Errorf("Password can't be a commonly used password")
2023
}
2124
}

common_password_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package validator
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
func ExampleCommonPassword() {
6-
passwordValidator := CommonPassword()
9+
passwordValidator := CommonPassword(nil)
710
fmt.Println(passwordValidator("password"))
811
fmt.Println(passwordValidator("qaz123"))
912
fmt.Println(passwordValidator("pa$$w0rd@"))
@@ -12,3 +15,11 @@ func ExampleCommonPassword() {
1215
// Password can't be a commonly used password
1316
// <nil>
1417
}
18+
19+
func ExampleCommonPassword_customError() {
20+
err := errors.New("custom error message")
21+
passwordValidator := CommonPassword(err)
22+
fmt.Println(passwordValidator("password"))
23+
// Output:
24+
// custom error message
25+
}

contains_at_least.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// ContainsAtLeast returns a ValidateFunc that count occurrences of a chars and compares it with required value
9-
func ContainsAtLeast(chars string, occurrences int) ValidateFunc {
9+
func ContainsAtLeast(chars string, occurrences int, customError error) ValidateFunc {
1010
return ValidateFunc(func(password string) error {
1111
cnt := 0
1212
aPassword := strings.Split(password, "")
@@ -18,6 +18,9 @@ func ContainsAtLeast(chars string, occurrences int) ValidateFunc {
1818
}
1919
}
2020
if cnt < occurrences {
21+
if customError != nil {
22+
return customError
23+
}
2124
return fmt.Errorf("Password must contains at least %d chars from %s", occurrences, chars)
2225
}
2326
return nil

contains_at_least_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package validator
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
func ExampleContainsAtLeast() {
6-
passwordValidator := ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4)
9+
passwordValidator := ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, nil)
710
fmt.Println(passwordValidator("password"))
811
fmt.Println(passwordValidator("PASSWORD"))
912
fmt.Println(passwordValidator("passWORD"))
@@ -12,3 +15,11 @@ func ExampleContainsAtLeast() {
1215
// Password must contains at least 4 chars from abcdefghijklmnopqrstuvwxyz
1316
// <nil>
1417
}
18+
19+
func ExampleContainsAtLeast_customError() {
20+
err := errors.New("custom error message")
21+
passwordValidator := ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, err)
22+
fmt.Println(passwordValidator("PASSWORD"))
23+
// Output:
24+
// custom error message
25+
}

max_length.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package validator
33
import "fmt"
44

55
// MaxLength returns a ValidateFunc that check if password length is not greater that "length"
6-
func MaxLength(length int) ValidateFunc {
6+
func MaxLength(length int, customError error) ValidateFunc {
77
return ValidateFunc(func(password string) error {
88
if len(password) > length {
9+
if customError != nil {
10+
return customError
11+
}
912
return fmt.Errorf("Password length must be not greater that %d chars", length)
1013
}
1114
return nil

max_length_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package validator
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
func ExampleMaxLength() {
6-
passwordValidator := MaxLength(5)
9+
passwordValidator := MaxLength(5, nil)
710
fmt.Println(passwordValidator("password"))
811
fmt.Println(passwordValidator("pass"))
912
fmt.Println(passwordValidator("passw"))
@@ -12,3 +15,11 @@ func ExampleMaxLength() {
1215
// <nil>
1316
// <nil>
1417
}
18+
19+
func ExampleMaxLength_customError() {
20+
err := errors.New("custom error message")
21+
passwordValidator := MaxLength(5, err)
22+
fmt.Println(passwordValidator("password"))
23+
// Output:
24+
// custom error message
25+
}

min_length.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package validator
33
import "fmt"
44

55
// MinLength returns a ValidateFunc that check if password length is not lower that "length"
6-
func MinLength(length int) ValidateFunc {
6+
func MinLength(length int, customError error) ValidateFunc {
77
return ValidateFunc(func(password string) error {
88
if len(password) < length {
9+
if customError != nil {
10+
return customError
11+
}
912
return fmt.Errorf("Password length must be not lower that %d chars", length)
1013
}
1114
return nil

min_length_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package validator
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
func ExampleMinLength() {
6-
passwordValidator := MinLength(5)
9+
passwordValidator := MinLength(5, nil)
710
fmt.Println(passwordValidator("password"))
811
fmt.Println(passwordValidator("pass"))
912
fmt.Println(passwordValidator("passw"))
@@ -12,3 +15,11 @@ func ExampleMinLength() {
1215
// Password length must be not lower that 5 chars
1316
// <nil>
1417
}
18+
19+
func ExampleMinLength_customError() {
20+
err := errors.New("custom error message")
21+
passwordValidator := MinLength(5, err)
22+
fmt.Println(passwordValidator("pass"))
23+
// Output:
24+
// custom error message
25+
}

noop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package validator
22

3-
// Noop returns a ValidateFunc that always truth
4-
func Noop() ValidateFunc {
3+
// Noop returns a ValidateFunc that always return custom error
4+
func Noop(customError error) ValidateFunc {
55
return ValidateFunc(func(password string) error {
6-
return nil
6+
return customError
77
})
88
}

0 commit comments

Comments
 (0)