golang中需要对struct结构体某些字段强制检查时, 可通过以下3中方式


1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
"fmt"
"gopkg.in/go-playground/validator.v9"
)

// 结构体检查必要字段
type User struct {
Email string `json:"email" validate:"required,email"`
Name string `json:"name" validate:"required"`
}

func main() {
v := validator.New()
a := User{
Email: "a",
Name: "Raymond",
}
err := v.Struct(a)
fmt.Println(err)
}

2.

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
package user

import (
"encoding/json"
"gopkg.in/go-playground/validator.v9"
)

type createUserRequest struct {
Email string `json:"email" validate:"required,email"`
Name string `json:"name" validate:"required,min=2,max=50"`
Password string `json:"name" validate:"required,min=8"`
}

// service 与检查逻辑分类, 通过实现handler关联相关操作
type UserHandler struct {
validate *validator.Validate
userService Service
// all other dependencies
}

// 接受需要检查的service struct 与 validate, 实现handler相关方法进行检查
func NewUserHandler(s Service, v *validator.Validate, /* all other dependencies */) UserHandler {
return UserHandler{userService: s, validate: v}
}

// Different frameworks do this differently. For simplicity let's assume
// we have built-in http server
func (h UserHandler) Create(req *http.Request) error {
input := createUserRequest{}

// unmarshal incoming input into pre-defined structure
defer req.Body.Close()
if err := json.NewEncoder(req.Body).Encode(&input) {
return err
}

// Just terminate the request if the input is not valid
if err := h.validate.Struct(input); err != nil {
return err
}

// Map to domain entity
user := User {
Email: input.Email,
Name: input.Name,
Password: input.Password,
}

// Finally pass the entity into service to get the job done!
return h.userService.Create(user)
}

3.

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
package main

import (
"fmt"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
"net"
)

// 适用于sql model的类型检查, 关联orm
type User struct {
Email string `json:"email" validate:"required,email"`
Name string `json:"name" validate:"required"`
}

type Customer struct {
Name string
Gender string
Email string
Address net.Addr
}

func (c Customer) Validate() error {
return validation.ValidateStruct(&c,
// Name cannot be empty, and the length must be between 5 and 20.
validation.Field(&c.Name, validation.Required, validation.Length(5, 20)),
// Gender is optional, and should be either "Female" or "Male".
validation.Field(&c.Gender, validation.In("Female", "Male")),
// Email cannot be empty and should be in a valid email format.
validation.Field(&c.Email, validation.Required, is.Email),
// Validate Address using its own validation rules
validation.Field(&c.Address),
)
}

func main() {
c := &Customer{
Name: "Raymond",
Gender: "Male",
Email: "liujinjia@wifi.com",
Address: nil,
}
err := c.Validate()
fmt.Println(err)
}