Skip to content

Commit

Permalink
Merge pull request #1273 from Chagui-/master
Browse files Browse the repository at this point in the history
Block Object: Validate text min and max length
  • Loading branch information
parsley42 committed Mar 20, 2024
2 parents af783b3 + fddc1a6 commit d2493fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions block_object.go
Expand Up @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions block_object_test.go
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d2493fa

Please sign in to comment.