@@ -16,7 +16,19 @@ go get -u github.com/go-passwd/validator
16
16
~~~ go
17
17
import " github.com/go-passwd/validator"
18
18
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" )))
20
32
err := passwordValidator.Validate (form.Password )
21
33
if err != nil {
22
34
panic (err)
@@ -30,23 +42,23 @@ if err != nil {
30
42
Check if password length is not lower that defined length.
31
43
32
44
~~~ go
33
- passwordValidator := validator.New (validator.MinLength (5 ))
45
+ passwordValidator := validator.New (validator.MinLength (5 , nil ))
34
46
~~~
35
47
36
48
### MaxLength
37
49
38
50
Check if password length is not greater that defined length.
39
51
40
52
~~~ go
41
- passwordValidator := validator.New (validator.MaxLength (10 ))
53
+ passwordValidator := validator.New (validator.MaxLength (10 , nil ))
42
54
~~~
43
55
44
56
### ContainsAtLeast
45
57
46
58
Count occurrences of a chars and compares it with required value.
47
59
48
60
~~~ go
49
- passwordValidator := validator.New (validator.ContainsAtLeast (5 , " abcdefghijklmnopqrstuvwxyz" ))
61
+ passwordValidator := validator.New (validator.ContainsAtLeast (5 , " abcdefghijklmnopqrstuvwxyz" , nil ))
50
62
~~~
51
63
52
64
### CommonPassword
@@ -56,13 +68,13 @@ Check if password is a common password.
56
68
Common password list is based on list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/
57
69
58
70
~~~ go
59
- passwordValidator := validator.New (validator.CommonPassword ())
71
+ passwordValidator := validator.New (validator.CommonPassword (nil ))
60
72
~~~
61
73
62
74
### Regex
63
75
64
76
Check if password match regexp pattern.
65
77
66
78
~~~ go
67
- passwordValidator := validator.New (validator.Regex (" ^\\ w+$" ))
79
+ passwordValidator := validator.New (validator.Regex (" ^\\ w+$" , nil ))
68
80
~~~
0 commit comments