-
Notifications
You must be signed in to change notification settings - Fork 6
/
validator.go
56 lines (46 loc) · 991 Bytes
/
validator.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
package dstruct
import (
"reflect"
"github.com/go-playground/validator/v10"
)
/***************************
@author: tiansheng.ren
@date: 2022/10/17
@desc:
***************************/
type validatorType func(value reflect.Value) error
var validate = validator.New()
// validateStruct 不会递归调用
func validateStruct(value reflect.Value) error {
if value.Interface() == nil || value.IsZero() {
return nil
}
data := value.Interface()
if data == nil {
return nil
}
if v, ok := data.(Validate); ok {
if err := v.Validate(); err != nil {
return err
}
}
switch value.Kind() {
case reflect.Ptr:
data := value.Elem().Interface()
if data == nil {
return nil
}
return validate.Struct(data)
case reflect.Struct:
return validate.Struct(data)
case reflect.Slice, reflect.Array:
for i := 0; i < value.Len(); i++ {
if err := validateStruct(value.Index(i)); err != nil {
return err
}
}
return nil
default:
return nil
}
}