Add validation where GUIDs are expected
Closed this issue · 3 comments
tjarrettveracode commented
Seeing some confusion between integer app IDs and GUIDs among users of the library. Should look into properly validating when one vs the other is expected.
tjarrettveracode commented
Sample GUID validation method by a Veracoder. Is there a better way?
def is_guid(str):
# Regex to check valid
# GUID (Globally Unique Identifier)
regex = "^[{]?[0-9a-fA-F]{8}" + "-([0-9a-fA-F]{4}-)" + "{3}[0-9a-fA-F]{12}[}]?$"
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if(re.search(p, str)):
return True
else:
return False
tjarrettveracode commented
Hah, I already wrote a method for this, at the cost of an additional import:
from uuid import UUID
def is_valid_uuid(uuid_to_test, version=4):
try:
uuid_obj = UUID(uuid_to_test, version=version)
except ValueError:
return False
return str(uuid_obj) == uuid_to_test
tjarrettveracode commented
Instead of validation, going to use argument typing as it's cleaner…