Skip to main content

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

ArgumentTypeDescription
object_or_objectsUnion[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_noneboolwhether or not None is a valid input (and thus output) option.
min_sizeOptional[int]if provided, the length of object_or_objects cannot be smaller than this.
max_sizeOptional[int]if provided, the length of object_or_objects cannot be larger than this.

Returns

TypeDescription
List[T]object_or_objects if it was a valid list, [object_or_objects] if it was a valid
object, or None if provided and allowed.

Errors

ErrorWhen
InvalidUsageErrorif 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

ArgumentTypeDescription
stringstrthe string to examine for hex characters.

Returns

TypeDescription
boolTrue 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

ArgumentTypeDescription
action_idstrthe action_id string to validate for correctness as per the Slack API.
allow_noneboolwhether to accept None as a valid value for action_id.

Returns

TypeDescription
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

ArgumentTypeDescription
numUnion[int, None]the number to validate.
min_valueOptional[int]if num is less than this value, an error will be thrown.
max_valueOptional[int]if num is greater than this value, an error will be thrown.
allow_noneboolwhether None is a valid value for num. If num is None
allow_none is False, an error will be thrown.

Returns

TypeDescription
intThe original value of num if it passes all validation checks.

Errors

ErrorWhen
InvalidUsageErrorif 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

ArgumentTypeDescription
stringOptional[str]the string to validate
field_namestrthe name of the field the string belongs to (for error reporting purposes).
min_lengthOptional[int]if the string is less than this length, an error will be raised.
max_lengthOptional[int]if the string is greated than this length, an error will be raised.
allow_noneboolwhether None is a valid value for the string being validated.

Returns

TypeDescription
Optional[str]The original string if it deemed to be valid (i.e. no errors are thrown).

Errors

ErrorWhen
InvalidUsageErrorif any of the validation checks (length, None) fail.