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

+18-6
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

+4-1
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

+13-2
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

+4-1
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

+13-2
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

+4-1
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

+13-2
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

+4-1
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

+13-2
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

+3-3
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
}

noop_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package validator
22

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

58
func ExampleNoop() {
6-
passwordValidator := Noop()
9+
passwordValidator := Noop(nil)
710
fmt.Println(passwordValidator("password"))
811
// Output:
912
// <nil>
1013
}
14+
15+
func ExampleNoop_customError() {
16+
err := errors.New("custom error message")
17+
passwordValidator := Noop(err)
18+
fmt.Println(passwordValidator("password"))
19+
// Output:
20+
// custom error message
21+
}

regex.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
)
77

88
// Regex returns ValidateFunc that check if password match regexp pattern
9-
func Regex(pattern string) ValidateFunc {
9+
func Regex(pattern string, customError error) ValidateFunc {
1010
return ValidateFunc(func(password string) error {
1111
matched, err := regexp.MatchString(pattern, password)
1212
if err != nil {
1313
return err
1414
}
1515
if matched {
16+
if customError != nil {
17+
return customError
18+
}
1619
return fmt.Errorf("Password shouldn't match \"%s\" pattern", pattern)
1720
}
1821
return nil

regex_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package validator
22

33
import (
4+
"errors"
45
"fmt"
56
)
67

78
func ExampleRegex() {
8-
passwordValidator := Regex("^\\w+$")
9+
passwordValidator := Regex("^\\w+$", nil)
910
fmt.Println(passwordValidator("password"))
1011
fmt.Println(passwordValidator("pa$$w0rd"))
1112
// Output:
1213
// Password shouldn't match "^\w+$" pattern
1314
// <nil>
1415
}
16+
17+
func ExampleRegex_customError() {
18+
err := errors.New("custom error message")
19+
passwordValidator := Regex("^\\w+$", err)
20+
fmt.Println(passwordValidator("password"))
21+
// Output:
22+
// custom error message
23+
}

validator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func TestValidator_empty(t *testing.T) {
1616
var password = "password"
1717

1818
func ExampleNew() {
19-
New(MinLength(5), MaxLength(10))
19+
New(MinLength(5, nil), MaxLength(10, nil))
2020
}
2121

2222
func TestNewValidator(t *testing.T) {
23-
passwordValidator := New(MinLength(5), MaxLength(10))
23+
passwordValidator := New(MinLength(5, nil), MaxLength(10, nil))
2424
assert.Len(t, *passwordValidator, 2)
2525
assert.Nil(t, passwordValidator.Validate("password"))
2626
assert.Nil(t, passwordValidator.Validate("pass1"))
@@ -30,7 +30,7 @@ func TestNewValidator(t *testing.T) {
3030
}
3131

3232
func ExampleValidator_Validate() {
33-
passwordValidator := Validator{MinLength(5), MaxLength(10)}
33+
passwordValidator := Validator{MinLength(5, nil), MaxLength(10, nil)}
3434
fmt.Println(passwordValidator.Validate("password"))
3535
fmt.Println(passwordValidator.Validate("pass1"))
3636
fmt.Println(passwordValidator.Validate("password12"))

0 commit comments

Comments
 (0)