Skip to content

Commit 69ecce1

Browse files
Added validator for attachment urls
1 parent 09cf30b commit 69ecce1

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

api/pkg/validators/message_handler_validator.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ func (validator MessageHandlerValidator) ValidateMessageSend(ctx context.Context
106106
return result
107107
}
108108

109+
if len(request.Attachments) > 10 {
110+
result.Add("attachments", "you cannot attach more than 10 files to a single message")
111+
}
112+
113+
for i, attachment := range request.Attachments {
114+
if strings.TrimSpace(attachment.ContentType) == "" {
115+
result.Add("attachments", fmt.Sprintf("attachment at index %d is missing content_type", i))
116+
}
117+
118+
if strings.TrimSpace(attachment.URL) == "" {
119+
result.Add("attachments", fmt.Sprintf("attachment at index %d is missing url", i))
120+
} else {
121+
// Basic URL validation
122+
parsedURL, err := url.ParseRequestURI(attachment.URL)
123+
if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
124+
result.Add("attachments", fmt.Sprintf("attachment at index %d has an invalid url format", i))
125+
} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
126+
result.Add("attachments", fmt.Sprintf("attachment at index %d must use http or https scheme", i))
127+
}
128+
}
129+
}
130+
109131
if request.SendAt != nil && request.SendAt.After(time.Now().Add(480*time.Hour)) {
110132
result.Add("send_at", "the scheduled time cannot be more than 20 days (480 hours) in the future")
111133
}

0 commit comments

Comments
 (0)