From fddc1a622c2c0e3dc37b72728ea8b63d5dd775a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Pe=C3=B1aloza?= Date: Fri, 15 Mar 2024 12:13:23 -0300 Subject: [PATCH] validate text length --- block_object.go | 10 ++++++++++ block_object_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/block_object.go b/block_object.go index b39ff2287..a3e78d4ea 100644 --- a/block_object.go +++ b/block_object.go @@ -147,6 +147,16 @@ func (s TextBlockObject) Validate() error { return errors.New("emoji cannot be true in mrkdown") } + // https://api.slack.com/reference/block-kit/composition-objects#text__fields + if len(s.Text) == 0 { + return errors.New("text must have a minimum length of 1") + } + + // https://api.slack.com/reference/block-kit/composition-objects#text__fields + if len(s.Text) > 3000 { + return errors.New("text cannot be longer than 3000 characters") + } + return nil } diff --git a/block_object_test.go b/block_object_test.go index 9889fae41..1f4874a02 100644 --- a/block_object_test.go +++ b/block_object_test.go @@ -2,6 +2,7 @@ package slack import ( "errors" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -126,6 +127,24 @@ func TestValidateTextBlockObject(t *testing.T) { }, expected: errors.New("emoji cannot be true in mrkdown"), }, + { + input: TextBlockObject{ + Type: "mrkdwn", + Text: "", + Emoji: false, + Verbatim: false, + }, + expected: errors.New("text must have a minimum length of 1"), + }, + { + input: TextBlockObject{ + Type: "mrkdwn", + Text: strings.Repeat("a", 3001), + Emoji: false, + Verbatim: false, + }, + expected: errors.New("text cannot be longer than 3000 characters"), + }, } for _, test := range tests {