Utilities
coerce_to_list
Function
coerce_to_list(object_or_objects: Union[T, List[T]], class_: Union[Any, List[Any]], allow_none: bool = False, min_size: Optional[int] = None, max_size: Optional[int] = None) -> List[T]
Takes and object or list of objects and validates its contents, ensuring that the resulting object is a list.
Arguments
| Argument | Type | Description |
|---|---|---|
object_or_objects | Union[T, List[T]] | the Python object or objects to validate and convert to a list. |
class_ | Union[Any, List[Any]] | the Python type (or class) of objects expected in the list. |
allow_none | bool | whether or not None is a valid input (and thus output) option. |
min_size | Optional[int] | if provided, the length of object_or_objects cannot be smaller than this. |
max_size | Optional[int] | if provided, the length of object_or_objects cannot be larger than this. |
Returns
| Type | Description |
|---|---|
List[T] | object_or_objects if it was a valid list, [object_or_objects] if it was a validobject, or None if provided and allowed. |
Errors
| Error | When |
|---|---|
InvalidUsageError | if any of the validation checks fail. |
is_hex
Function
is_hex(string: str) -> bool
Determines whether a given string is a valid hexadecimal number.
Arguments
| Argument | Type | Description |
|---|---|---|
string | str | the string to examine for hex characters. |
Returns
| Type | Description |
|---|---|
bool | True if the string is a valid hexadecimal number, otherwise False. |
validate_action_id
Function
validate_action_id(action_id: str, allow_none: bool = False) -> Optional[str]
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
Arguments
| Argument | Type | Description |
|---|---|---|
action_id | str | the action_id string to validate for correctness as per the Slack API. |
allow_none | bool | whether to accept None as a valid value for action_id. |
Returns
| Type | Description |
|---|---|
Optional[str] | The original value action_id if all validation checks pass. |
validate_int
Function
validate_int(num: Union[int, None], min_value: Optional[int] = None, max_value: Optional[int] = None, allow_none: bool = False) -> int
Performs basic validation checks against a given integer.
Arguments
| Argument | Type | Description |
|---|---|---|
num | Union[int, None] | the number to validate. |
min_value | Optional[int] | if num is less than this value, an error will be thrown. |
max_value | Optional[int] | if num is greater than this value, an error will be thrown. |
allow_none | bool | whether None is a valid value for num. If num is Noneallow_none is False, an error will be thrown. |
Returns
| Type | Description |
|---|---|
int | The original value of num if it passes all validation checks. |
Errors
| Error | When |
|---|---|
InvalidUsageError | if any of the validation checks fail. |
validate_string
Function
validate_string(string: Optional[str], field_name: str, max_length: Optional[int] = None, min_length: Optional[int] = None, allow_none: bool = False) -> Optional[str]
Performs basic validation actions (e.g. length checking) on a given string based on the provided criteria.
Arguments
| Argument | Type | Description |
|---|---|---|
string | Optional[str] | the string to validate |
field_name | str | the name of the field the string belongs to (for error reporting purposes). |
min_length | Optional[int] | if the string is less than this length, an error will be raised. |
max_length | Optional[int] | if the string is greated than this length, an error will be raised. |
allow_none | bool | whether None is a valid value for the string being validated. |
Returns
| Type | Description |
|---|---|
Optional[str] | The original string if it deemed to be valid (i.e. no errors are thrown). |
Errors
| Error | When |
|---|---|
InvalidUsageError | if any of the validation checks (length, None) fail. |