Sanitize context label values
Python code example of how to ensure invalid characters are not part of a context label value being sent to the context labels API.
1
Check that replacement character is valid
If a replacement other than the default _ is specified, ensure that is valid itself
def sanitize_label(self, label, replacement_char="_", max_label_len=80):
if not label:
raise ValueError("Empty label string cannot be sanitized")
if replacement_char != "_":
valid_char_pattern = r"^[a-zA-Z0-9 ._/\\\-#~:()]$"
if not re.match(valid_char_pattern, replacement_char):
raise ValueError("Invalid replacement character")3
Last updated