Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Object: Validate text min and max length #1273

Merged
merged 1 commit into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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