diff options
author | Frederick Muriuki Muriithi | 2024-06-12 10:27:33 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-06-12 10:27:33 -0500 |
commit | a2344e3697bbff14b82bdbe5fa08a1cbaea09cbc (patch) | |
tree | 6815384a34ac78ef8ab4d714f7b91f42140ccebe | |
parent | 76eaf7c3ef2896f56bdc4b2ab154232df335ee8f (diff) | |
download | gn-uploader-a2344e3697bbff14b82bdbe5fa08a1cbaea09cbc.tar.gz |
Provide an input validation module.
-rw-r--r-- | qc_app/input_validation.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/qc_app/input_validation.py b/qc_app/input_validation.py new file mode 100644 index 0000000..96c2f05 --- /dev/null +++ b/qc_app/input_validation.py @@ -0,0 +1,27 @@ +"""Input validation utilities""" +from typing import Any + +def is_empty_string(value: str) -> bool: + """Check whether as string is empty""" + return (isinstance(value, str) and value.strip() == "") + +def is_empty_input(value: Any) -> bool: + """Check whether user provided an empty value.""" + return (value is None or is_empty_string(value)) + +def is_integer_input(value: Any) -> bool: + """ + Check whether user provided a value that can be parsed into an integer. + """ + def __is_int__(val, base): + try: + int(val, base=10) + except ValueError as verr: + return False + return True + return isinstance(value, int) or ( + (not is_empty_input(value)) and ( + isinstance(str) and ( + __is_int__(value, 10) + or __is_int__(value, 8) + or __is_int__(value, 16)))) |