-
Notifications
You must be signed in to change notification settings - Fork 25
/
validator.go
96 lines (86 loc) · 2.46 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
package config
import (
"fmt"
"reflect"
validator "github.com/go-playground/validator/v10"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/fluxninja/aperture/pkg/log"
)
var globalValidate = getValidate()
func getValidate() *validator.Validate {
validate := validator.New()
validate.RegisterCustomTypeFunc(durationCustomTypeFunc, Duration{})
validate.RegisterCustomTypeFunc(durationpbCustomTypeFunc, durationpb.Duration{})
validate.RegisterCustomTypeFunc(timestampCustomTypeFunc, Time{})
validate.RegisterCustomTypeFunc(timestamppbCustomTypeFunc, timestamppb.Timestamp{})
return validate
}
// ValidateStruct takes interface value and validates its fields of a struct.
func ValidateStruct(rawVal interface{}) error {
// validate configuration
err := globalValidate.Struct(rawVal)
if err != nil {
if _, ok := err.(*validator.InvalidValidationError); ok {
log.Panic().Err(err).Msg("InvalidValidationError!")
} else if _, ok := err.(validator.ValidationErrors); ok {
for _, err := range err.(validator.ValidationErrors) {
errorStr := fmt.Sprintf("ValidationError<"+
"Namespace: %s"+
"| Field: %s"+
"| StructNamespace: %s"+
"| StructField: %s"+
"| Tag: %s"+
"| ActualTag: %s"+
"| Kind: %s"+
"| Type: %s"+
"| Value: %s"+
"| Param: %s"+
">",
err.Namespace(),
err.Field(),
err.StructNamespace(),
err.StructField(),
err.Tag(),
err.ActualTag(),
err.Kind(),
err.Type(),
err.Value(),
err.Param())
log.Error().Err(err).Msg(errorStr)
}
}
}
return err
}
func durationCustomTypeFunc(field reflect.Value) interface{} {
if value, ok := field.Interface().(Duration); ok {
return value.AsDuration()
}
return nil
}
func durationpbCustomTypeFunc(field reflect.Value) interface{} {
iface := field.Interface()
switch iface.(type) {
case durationpb.Duration:
ptr := field.Addr().Interface()
return ptr.(*durationpb.Duration).AsDuration()
}
return nil
}
func timestampCustomTypeFunc(field reflect.Value) interface{} {
if value, ok := field.Interface().(Time); ok {
return value.timestamp.AsTime()
}
return nil
}
func timestamppbCustomTypeFunc(field reflect.Value) interface{} {
iface := field.Interface()
switch iface.(type) {
case timestamppb.Timestamp:
log.Debug().Msg("timestamppbCustomTypeFunc")
ptr := field.Addr().Interface()
return ptr.(*timestamppb.Timestamp).AsTime()
}
return nil
}