-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
47 lines (41 loc) · 1.12 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package data
import "github.com/go-playground/validator/v10"
//User represents the user data.
type User struct {
Name string `json:"name" validate:"min=3,max=64"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"min=8,max=32,password"`
}
//Auth represents authentication data.
type Auth struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"min=8,max=32,password"`
}
//RegisterPasswordValidator registers validator for password.
func RegisterPasswordValidator(validate *validator.Validate) {
validate.RegisterValidation("password", func(fl validator.FieldLevel) bool {
return isPasswordValid(fl.Field().String())
})
}
func isPasswordValid(s string) bool {
digit := false
lowerCase := false
upperCase := false
for _, c := range s {
if inRange(c, '0', '9') {
digit = true
continue
}
if inRange(c, 'a', 'z') {
lowerCase = true
continue
}
if inRange(c, 'A', 'Z') {
upperCase = true
}
}
return digit && lowerCase && upperCase
}
func inRange(x, a, b rune) bool {
return x >= a && x <= b
}