Skip to content

Commit d48eb82

Browse files
author
Jannis Pohlmann
committed
subscriptions: Add extra validation for subscriptions
1 parent ddef7f6 commit d48eb82

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

subscriptions.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ func (m *subscriptionManager) AddSubscription(
111111
"subscription": subscription.ID,
112112
}).Info("Add subscription")
113113

114+
if errors := validateSubscription(subscription); len(errors) > 0 {
115+
m.logger.WithField("errors", errors).Warn("Failed to add invalid subscription")
116+
return errors
117+
}
118+
114119
// Parse the subscription query
115120
document, err := parser.Parse(parser.ParseParams{
116121
Source: subscription.Query,
@@ -184,3 +189,25 @@ func (m *subscriptionManager) RemoveSubscriptions(conn Connection) {
184189
delete(m.subscriptions, conn)
185190
}
186191
}
192+
193+
func validateSubscription(s *Subscription) []error {
194+
errs := []error{}
195+
196+
if s.ID == "" {
197+
errs = append(errs, errors.New("Subscription ID is empty"))
198+
}
199+
200+
if s.Connection == nil {
201+
errs = append(errs, errors.New("Subscription is not associated with a connection"))
202+
}
203+
204+
if s.Query == "" {
205+
errs = append(errs, errors.New("Subscription query is empty"))
206+
}
207+
208+
if s.SendData == nil {
209+
errs = append(errs, errors.New("Subscription has no SendData function set"))
210+
}
211+
212+
return errs
213+
}

0 commit comments

Comments
 (0)