Skip to content

Commit

Permalink
[ENH] Add length validation to Text object (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklambourne authored Nov 28, 2023
1 parent b8af8d5 commit b310369
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion slackblocks/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def __init__(
):
super().__init__(type_=CompositionObjectType.TEXT)
self.text_type = type_
self.text = text
self.text = validate_string(
text, field_name="text", min_length=1, max_length=3000
)
if self.text_type == TextType.MARKDOWN:
self.verbatim = verbatim
self.emoji = None
Expand Down
6 changes: 6 additions & 0 deletions slackblocks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def validate_string(
string: Optional[str],
field_name: str,
max_length: Optional[int] = None,
min_length: Optional[int] = None,
allow_none: bool = False,
) -> Optional[str]:
if string is None:
Expand All @@ -83,6 +84,11 @@ def validate_string(
)
else:
length = len(string)
if min_length and length < min_length:
raise InvalidUsageError(
f"Argument to field `{field_name}` ({length} characters) "
f"is less than minimum length of {max_length} characters"
)
if max_length and length > max_length:
raise InvalidUsageError(
f"Argument to field `{field_name}` ({length} characters) "
Expand Down

0 comments on commit b310369

Please sign in to comment.