-
Notifications
You must be signed in to change notification settings - Fork 487
/
validators.go
86 lines (75 loc) · 2.78 KB
/
validators.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package users
import (
"github.com/gothinkster/golang-gin-realworld-example-app/common"
"github.com/gin-gonic/gin"
)
// *ModelValidator containing two parts:
// - Validator: write the form/json checking rule according to the doc https://github.com/go-playground/validator
// - DataModel: fill with data from Validator after invoking common.Bind(c, self)
// Then, you can just call model.save() after the data is ready in DataModel.
type UserModelValidator struct {
User struct {
Username string `form:"username" json:"username" binding:"exists,alphanum,min=4,max=255"`
Email string `form:"email" json:"email" binding:"exists,email"`
Password string `form:"password" json:"password" binding:"exists,min=8,max=255"`
Bio string `form:"bio" json:"bio" binding:"max=1024"`
Image string `form:"image" json:"image" binding:"omitempty,url"`
} `json:"user"`
userModel UserModel `json:"-"`
}
// There are some difference when you create or update a model, you need to fill the DataModel before
// update so that you can use your origin data to cheat the validator.
// BTW, you can put your general binding logic here such as setting password.
func (self *UserModelValidator) Bind(c *gin.Context) error {
err := common.Bind(c, self)
if err != nil {
return err
}
self.userModel.Username = self.User.Username
self.userModel.Email = self.User.Email
self.userModel.Bio = self.User.Bio
if self.User.Password != common.NBRandomPassword {
self.userModel.setPassword(self.User.Password)
}
if self.User.Image != "" {
self.userModel.Image = &self.User.Image
}
return nil
}
// You can put the default value of a Validator here
func NewUserModelValidator() UserModelValidator {
userModelValidator := UserModelValidator{}
//userModelValidator.User.Email ="w@g.cn"
return userModelValidator
}
func NewUserModelValidatorFillWith(userModel UserModel) UserModelValidator {
userModelValidator := NewUserModelValidator()
userModelValidator.User.Username = userModel.Username
userModelValidator.User.Email = userModel.Email
userModelValidator.User.Bio = userModel.Bio
userModelValidator.User.Password = common.NBRandomPassword
if userModel.Image != nil {
userModelValidator.User.Image = *userModel.Image
}
return userModelValidator
}
type LoginValidator struct {
User struct {
Email string `form:"email" json:"email" binding:"exists,email"`
Password string `form:"password"json:"password" binding:"exists,min=8,max=255"`
} `json:"user"`
userModel UserModel `json:"-"`
}
func (self *LoginValidator) Bind(c *gin.Context) error {
err := common.Bind(c, self)
if err != nil {
return err
}
self.userModel.Email = self.User.Email
return nil
}
// You can put the default value of a Validator here
func NewLoginValidator() LoginValidator {
loginValidator := LoginValidator{}
return loginValidator
}