Skip to content

Commit

Permalink
Check for valid E.164 format in phone numbers (#34)
Browse files Browse the repository at this point in the history
* Check for valid E.164 format

* address review comments

* retrigger CI
  • Loading branch information
ndodda-amazon committed Dec 18, 2020
1 parent 7ce21d3 commit 9020cdd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion smdebug_rulesconfig/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def __init__(self, phone_number: str):
}
```
:param phone_number: 10-15 digit phone number preceded by a `+`.
:param phone_number: Valid phone number that follows the the E.164 format. See
https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html for more info.
"""
validate_phone_number("phone_number", phone_number)
super(SMS, self).__init__(endpoint=phone_number)
Expand Down
18 changes: 7 additions & 11 deletions smdebug_rulesconfig/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
import json


TRAINING_JOB_PREFIX_REGEX = "[A-Za-z0-9\-]+"
TRAINING_JOB_PREFIX_REGEX = "^[A-Za-z0-9\-]+$"
EMAIL_ADDRESS_REGEX = "^[a-z0-9]+[@]\w+[.]\w{2,3}$"
PHONE_NUMBER_REGEX = "\+\d{10,15}"


def _match_regex(regex, string):
match = re.search(regex, string)
return match is not None and match.start() == 0 and match.end() == len(string)
PHONE_NUMBER_REGEX = "^\+\d{1,15}$"


def validate_training_job_prefix(key, value):
if not isinstance(value, str):
raise ValueError(f"{key} must be a string!")
if not _match_regex(TRAINING_JOB_PREFIX_REGEX, value):
if not re.match(TRAINING_JOB_PREFIX_REGEX, value):
raise ValueError(
"Invalid training job prefix! Must contain only letters, numbers and hyphens!"
)
Expand All @@ -24,16 +19,17 @@ def validate_training_job_prefix(key, value):
def validate_email_address(key, value):
if not isinstance(value, str):
raise ValueError(f"{key} must be a string!")
if not _match_regex(EMAIL_ADDRESS_REGEX, value):
if not re.match(EMAIL_ADDRESS_REGEX, value):
raise ValueError("Invalid email address provided! Must follow this scheme: username@domain")


def validate_phone_number(key, value):
if not isinstance(value, str):
raise ValueError(f"{key} must be a string!")
if not _match_regex(PHONE_NUMBER_REGEX, value):
if not re.match(PHONE_NUMBER_REGEX, value):
raise ValueError(
"Invalid phone number provided! Must be 10-15 digit number starting with a `+`!"
"""Invalid phone number provided! Must follow the E.164 format.
See https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html for more info."""
)


Expand Down

0 comments on commit 9020cdd

Please sign in to comment.