Skip to content

Commit 2d83529

Browse files
authored
fix: Publish API validation (#272)
* fix: Publish API validate required tenant_id * fix: Improve topics validation
1 parent d99f0a8 commit 2d83529

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

internal/publishmq/eventhandler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
var (
21-
ErrInvalidTopic = errors.New("invalid topic")
21+
ErrInvalidTopic = errors.New("invalid topic")
22+
ErrRequiredTopic = errors.New("topic is required")
2223
)
2324

2425
type EventHandler interface {
@@ -62,6 +63,9 @@ func NewEventHandler(
6263
var _ EventHandler = (*eventHandler)(nil)
6364

6465
func (h *eventHandler) Handle(ctx context.Context, event *models.Event) error {
66+
if len(h.topics) > 0 && event.Topic == "" {
67+
return ErrRequiredTopic
68+
}
6569
if len(h.topics) > 0 && event.Topic != "*" && !slices.Contains(h.topics, event.Topic) {
6670
return ErrInvalidTopic
6771
}

internal/services/api/publish_handlers.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,24 @@ func (h *PublishHandlers) Ingest(c *gin.Context) {
3838
if err := h.eventHandler.Handle(c.Request.Context(), &event); err != nil {
3939
if errors.Is(err, idempotence.ErrConflict) {
4040
c.Status(http.StatusConflict)
41+
} else if errors.Is(err, publishmq.ErrRequiredTopic) {
42+
AbortWithValidationError(c, ErrorResponse{
43+
Code: http.StatusUnprocessableEntity,
44+
Message: "validation error",
45+
Err: err,
46+
Data: map[string]string{
47+
"topic": "required",
48+
},
49+
})
4150
} else if errors.Is(err, publishmq.ErrInvalidTopic) {
42-
AbortWithValidationError(c, err)
51+
AbortWithValidationError(c, ErrorResponse{
52+
Code: http.StatusUnprocessableEntity,
53+
Message: "validation error",
54+
Err: err,
55+
Data: map[string]string{
56+
"topic": "invalid",
57+
},
58+
})
4359
} else {
4460
AbortWithError(c, http.StatusInternalServerError, NewErrInternalServer(err))
4561
}
@@ -48,10 +64,9 @@ func (h *PublishHandlers) Ingest(c *gin.Context) {
4864
c.Status(http.StatusOK)
4965
}
5066

51-
// TODO: validation
5267
type PublishedEvent struct {
5368
ID string `json:"id"`
54-
TenantID string `json:"tenant_id"`
69+
TenantID string `json:"tenant_id" binding:"required"`
5570
DestinationID string `json:"destination_id"`
5671
Topic string `json:"topic"`
5772
EligibleForRetry bool `json:"eligible_for_retry"`

0 commit comments

Comments
 (0)