-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
138 lines (118 loc) · 4.56 KB
/
validation.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package driplimit
import (
"context"
"reflect"
"strings"
"github.com/go-playground/validator/v10"
)
// Validator is a validator for the Driplimit service. It implements the Driplimit interface.
type Validator struct {
validator *validator.Validate
driplimit Service
}
func NewServiceValidator(driplimit Service) *Validator {
validator := validator.New()
// https://github.com/go-playground/validator/issues/258#issuecomment-257281334
validator.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
return &Validator{
validator: validator,
driplimit: driplimit,
}
}
// KeyCheck validates the payload and calls the KeyCheck method of the wrapped Driplimit service.
func (v *Validator) KeyCheck(ctx context.Context, payload KeysCheckPayload) (key *Key, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyCheck(ctx, payload)
}
// KeyCreate validates the payload and calls the KeyCreate method of the wrapped Driplimit service.
func (v *Validator) KeyCreate(ctx context.Context, payload KeyCreatePayload) (key *Key, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyCreate(ctx, payload)
}
// KeyGet validates the payload and calls the KeyGet method of the wrapped Driplimit service.
func (v *Validator) KeyGet(ctx context.Context, payload KeyGetPayload) (key *Key, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyGet(ctx, payload)
}
// KeyList validates the payload and calls the KeyList method of the wrapped Driplimit service.
func (v *Validator) KeyList(ctx context.Context, payload KeyListPayload) (klist *KeyList, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyList(ctx, payload)
}
// KeyDelete validates the payload and calls the KeyDelete method of the wrapped Driplimit service.
func (v *Validator) KeyDelete(ctx context.Context, payload KeyDeletePayload) (err error) {
if err := payload.Validate(v.validator); err != nil {
return err
}
return v.driplimit.KeyDelete(ctx, payload)
}
// KeyspaceGet validates the payload and calls the KeyspaceGet method of the wrapped Driplimit service.
func (v *Validator) KeyspaceGet(ctx context.Context, payload KeyspaceGetPayload) (keyspace *Keyspace, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyspaceGet(ctx, payload)
}
// KeyspaceCreate validates the payload and calls the KeyspaceCreate method of the wrapped Driplimit service.
func (v *Validator) KeyspaceCreate(ctx context.Context, payload KeyspaceCreatePayload) (keyspace *Keyspace, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyspaceCreate(ctx, payload)
}
func (v *Validator) KeyspaceList(ctx context.Context, payload KeyspaceListPayload) (kslist *KeyspaceList, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.KeyspaceList(ctx, payload)
}
func (v *Validator) KeyspaceDelete(ctx context.Context, payload KeyspaceDeletePayload) (err error) {
if err := payload.Validate(v.validator); err != nil {
return err
}
return v.driplimit.KeyspaceDelete(ctx, payload)
}
func (v *Validator) ServiceKeyGet(ctx context.Context, payload ServiceKeyGetPayload) (sk *ServiceKey, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.ServiceKeyGet(ctx, payload)
}
func (v *Validator) ServiceKeyCreate(ctx context.Context, payload ServiceKeyCreatePayload) (sk *ServiceKey, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.ServiceKeyCreate(ctx, payload)
}
func (v *Validator) ServiceKeyList(ctx context.Context, payload ServiceKeyListPayload) (sklist *ServiceKeyList, err error) {
if err := payload.Validate(v.validator); err != nil {
return nil, err
}
return v.driplimit.ServiceKeyList(ctx, payload)
}
func (v *Validator) ServiceKeyDelete(ctx context.Context, payload ServiceKeyDeletePayload) (err error) {
if err := payload.Validate(v.validator); err != nil {
return err
}
return v.driplimit.ServiceKeyDelete(ctx, payload)
}
func (v *Validator) ServiceKeySetToken(ctx context.Context, payload ServiceKeySetTokenPayload) (err error) {
if err := payload.Validate(v.validator); err != nil {
return err
}
return v.driplimit.ServiceKeySetToken(ctx, payload)
}