From b31036940fe68047c226fadac995bac4e8e8fa47 Mon Sep 17 00:00:00 2001 From: Nicholas Lambourne Date: Tue, 28 Nov 2023 20:37:32 +1000 Subject: [PATCH] [ENH] Add length validation to Text object (#55) --- slackblocks/objects.py | 4 +++- slackblocks/utils.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/slackblocks/objects.py b/slackblocks/objects.py index fb8c0ad..4c98c0f 100644 --- a/slackblocks/objects.py +++ b/slackblocks/objects.py @@ -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 diff --git a/slackblocks/utils.py b/slackblocks/utils.py index 1b5d455..06159f3 100644 --- a/slackblocks/utils.py +++ b/slackblocks/utils.py @@ -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: @@ -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) "