Skip to content

Commit 681a29f

Browse files
committed
chore: require both title and message
1 parent 657a230 commit 681a29f

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

coderd/notifications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (api *API) postCustomNotification(rw http.ResponseWriter, r *http.Request)
367367
return
368368
}
369369

370-
// Validate request: require `content` and at least one non-empty `title` or `message`.
370+
// Validate request: require `content` and non-empty `title` and `message`
371371
if err := req.Validate(); err != nil {
372372
api.Logger.Error(ctx, "send custom notification: validation failed", slog.Error(err))
373373
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{

codersdk/notifications.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,20 @@ func (c CustomNotificationRequest) Validate() error {
302302
if c.Content == nil {
303303
return xerrors.Errorf("content is required")
304304
}
305-
if strings.TrimSpace(c.Content.Title) == "" &&
306-
strings.TrimSpace(c.Content.Message) == "" {
307-
return xerrors.Errorf("provide a non-empty 'content.title' or 'content.message'")
308-
}
305+
return c.Content.Validate()
306+
}
309307

310-
if len(c.Content.Title) > maxCustomNotificationTitleLen {
308+
func (c CustomNotificationContent) Validate() error {
309+
if strings.TrimSpace(c.Title) == "" ||
310+
strings.TrimSpace(c.Message) == "" {
311+
return xerrors.Errorf("provide a non-empty 'content.title' and 'content.message'")
312+
}
313+
if len(c.Title) > maxCustomNotificationTitleLen {
311314
return xerrors.Errorf("'content.title' must be less than %d characters", maxCustomNotificationTitleLen)
312315
}
313-
if len(c.Content.Message) > maxCustomNotificationMessageLen {
316+
if len(c.Message) > maxCustomNotificationMessageLen {
314317
return xerrors.Errorf("'content.message' must be less than %d characters", maxCustomNotificationMessageLen)
315318
}
316-
317319
return nil
318320
}
319321

0 commit comments

Comments
 (0)