forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_handler_validator.go
More file actions
106 lines (98 loc) · 2.25 KB
/
webhook_handler_validator.go
File metadata and controls
106 lines (98 loc) · 2.25 KB
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
package validators
import (
"context"
"fmt"
"net/url"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/thedevsaddam/govalidator"
)
// WebhookHandlerValidator validates models used in handlers.WebhookHandler
type WebhookHandlerValidator struct {
validator
logger telemetry.Logger
tracer telemetry.Tracer
}
// NewWebhookHandlerValidator creates a new handlers.WebhookHandler validator
func NewWebhookHandlerValidator(
logger telemetry.Logger,
tracer telemetry.Tracer,
) (v *WebhookHandlerValidator) {
return &WebhookHandlerValidator{
logger: logger.WithService(fmt.Sprintf("%T", v)),
tracer: tracer,
}
}
// ValidateIndex validates the requests.HeartbeatIndex request
func (validator *WebhookHandlerValidator) ValidateIndex(_ context.Context, request requests.WebhookIndex) url.Values {
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"limit": []string{
"required",
"numeric",
"min:1",
"max:100",
},
"skip": []string{
"required",
"numeric",
"min:0",
},
"query": []string{
"max:100",
},
},
})
return v.ValidateStruct()
}
// ValidateStore validates the requests.WebhookStore request
func (validator *WebhookHandlerValidator) ValidateStore(_ context.Context, request requests.WebhookStore) url.Values {
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"signing_key": []string{
"required",
"min:1",
"max:255",
},
"url": []string{
"required",
"url",
"max:255",
},
"events": []string{
"required",
webhookEventsRule,
},
},
})
return v.ValidateStruct()
}
// ValidateUpdate validates the requests.WebhookUpdate request
func (validator *WebhookHandlerValidator) ValidateUpdate(_ context.Context, request requests.WebhookUpdate) url.Values {
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"signing_key": []string{
"required",
"min:1",
"max:255",
},
"webhookID": []string{
"required",
"uuid",
},
"url": []string{
"required",
"url",
"max:255",
},
"events": []string{
"required",
webhookEventsRule,
},
},
})
return v.ValidateStruct()
}