Utilities
This module collects various utility functions used for validating
the input to Messages
, Blocks
, Elements
and Objects
.
coerce_to_list
coerce_to_list(
object_or_objects,
class_,
allow_none=False,
min_size=None,
max_size=None,
)
Takes and object or list of objects and validates its contents, ensuring that the resulting object is a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object_or_objects |
Union[T, List[T]]
|
the Python object or objects to validate and convert to a list. |
required |
class_ |
Union[Any, List[Any]]
|
the Python type (or class) of objects expected in the list. |
required |
allow_none |
bool
|
whether or not None is a valid input (and thus output) option. |
False
|
min_size |
Optional[int]
|
if provided, the length of |
None
|
max_size |
Optional[int]
|
if provided, the length of |
None
|
Returns:
Type | Description |
---|---|
List[T]
|
|
Throws
InvalidUsageError: if any of the validation checks fail.
Source code in slackblocks/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
is_hex
is_hex(string)
Determines whether a given string is a valid hexadecimal number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
the string to examine for hex characters. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in slackblocks/utils.py
77 78 79 80 81 82 83 84 85 86 87 |
|
validate_action_id
validate_action_id(action_id, allow_none=False)
Action IDs are used in the handing of user interactivity within Slack blocks.
This function checks that a given action_id
is valid as per the requirements
imposed by the Slack API.
See: https://api.slack.com/interactivity/handling
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_id |
str
|
the action_id string to validate for correctness as per the Slack API. |
required |
allow_none |
bool
|
whether to accept |
False
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The original value |
Throws
InvalidUsageError if any of the validation checks fail.
Source code in slackblocks/utils.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
validate_int
validate_int(
num, min_value=None, max_value=None, allow_none=False
)
Performs basic validation checks against a given integer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
Union[int, None]
|
the number to validate. |
required |
min_value |
Optional[int]
|
if |
None
|
max_value |
Optional[int]
|
if |
None
|
allow_none |
bool
|
whether |
False
|
Returns:
Type | Description |
---|---|
int
|
The original value of |
Throws
InvalidUsageError: if any of the validation checks fail.
Source code in slackblocks/utils.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
validate_string
validate_string(
string,
field_name,
max_length=None,
min_length=None,
allow_none=False,
)
Performs basic validation actions (e.g. length checking) on a given string based on the provided criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
Optional[str]
|
the string to validate |
required |
field_name |
str
|
the name of the field the string belongs to (for error reporting purposes). |
required |
min_length |
Optional[int]
|
if the string is less than this length, an error will be raised. |
None
|
max_length |
Optional[int]
|
if the string is greated than this length, an error will be raised. |
None
|
allow_none |
bool
|
whether |
False
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The original string if it deemed to be valid (i.e. no errors are thrown). |
Throws
InvalidUsageError: if any of the validation checks (length, None
) fail.
Source code in slackblocks/utils.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|