mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
update validator
This commit is contained in:
parent
e1fd945037
commit
a0e5c69c65
@ -81,13 +81,16 @@ func register(req RegisterReq) error{
|
|||||||
import "gopkg.in/go-playground/validator.v9"
|
import "gopkg.in/go-playground/validator.v9"
|
||||||
|
|
||||||
type RegisterReq struct {
|
type RegisterReq struct {
|
||||||
|
// 字符串的 gt=0 表示长度必须 > 0,gt = greater than
|
||||||
Username string `validate:"gt=0"`
|
Username string `validate:"gt=0"`
|
||||||
|
// 同上
|
||||||
PasswordNew string `validate:"gt=0"`
|
PasswordNew string `validate:"gt=0"`
|
||||||
|
// eqfield 跨字段相等校验
|
||||||
PasswordRepeat string `validate:"eqfield=PasswordNew"`
|
PasswordRepeat string `validate:"eqfield=PasswordNew"`
|
||||||
|
// 合法 email 格式校验
|
||||||
Email string `validate:"email"`
|
Email string `validate:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func validate(req RegisterReq) error {
|
func validate(req RegisterReq) error {
|
||||||
err := validate.Struct(mystruct)
|
err := validate.Struct(mystruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -98,6 +101,24 @@ func validate(req RegisterReq) error {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
这样就不需要在每个请求进入业务逻辑之前都写重复的 validate 函数了。如果觉得这个 validator 提供的错误信息不够人性化,也可以针对每种 tag 进行错误信息订制,读者可以自行探索。
|
这样就不需要在每个请求进入业务逻辑之前都写重复的 validate 函数了。本例中只列出了这个 validator 非常简单的几个功能。
|
||||||
|
|
||||||
|
我们试着跑一下这个程序,输入参数设置为:
|
||||||
|
|
||||||
|
```go
|
||||||
|
//...
|
||||||
|
|
||||||
|
var req = RegisterReq {
|
||||||
|
Username : "Xargin",
|
||||||
|
PasswordNew : "ohno",
|
||||||
|
PasswordRepeat : "ohn",
|
||||||
|
Email : "alex@abc.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := validate.Struct(mystruct)
|
||||||
|
fmt.Println(err) // Key: 'RegisterReq.PasswordRepeat' Error:Field validation for 'PasswordRepeat' failed on the 'eqfield' tag
|
||||||
|
```
|
||||||
|
|
||||||
|
如果觉得这个 validator 提供的错误信息不够人性化,例如要把错误信息返回给用户,那就不应该直接显示英文了。可以针对每种 tag 进行错误信息订制,读者可以自行探索。
|
||||||
|
|
||||||
## 原理
|
## 原理
|
||||||
|
Loading…
x
Reference in New Issue
Block a user