Skip to content

Commit 776e7db

Browse files
author
bobdev-94
authored
30397 nr phone validation (bcgov#3782)
* 30397 nr phone validation * clean up * add missing * fix lint
1 parent e01ae46 commit 776e7db

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

legal-api/src/legal_api/resources/v2/namerequest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from legal_api.services import namex
2323
from legal_api.services.bootstrap import AccountService
2424
from legal_api.services.permissions import ListActionsPermissionsAllowed, PermissionService
25+
from legal_api.utils.formatting import normalize_phone
2526

2627

2728
bp = Blueprint('NAMEREQUEST2', __name__, url_prefix='/api/v2/nameRequests')
@@ -65,7 +66,7 @@ def validate_with_contact_info(identifier):
6566
# If NR is not affiliated, validate the email and phone
6667
nr_phone = nr_json.get('applicants').get('phoneNumber')
6768
nr_email = nr_json.get('applicants').get('emailAddress')
68-
if (phone and phone != nr_phone) or (email and email != nr_email):
69+
if (phone and normalize_phone(phone) != normalize_phone(nr_phone)) or (email and email != nr_email):
6970
return make_response(jsonify(message='Invalid email or phone number.'), 400)
7071

7172
return jsonify(nr_json)

legal-api/src/legal_api/utils/formatting.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Custom formatting."""
1515
import decimal
16+
import re
1617

1718

1819
def float_to_str(f, precision=17):
@@ -22,3 +23,28 @@ def float_to_str(f, precision=17):
2223

2324
value = ctx.create_decimal(repr(f))
2425
return format(value, 'f')
26+
27+
28+
def normalize_phone(phone: str) -> str:
29+
"""
30+
Normalize a phone number by stripping all non-digit characters.
31+
32+
This function takes a phone number string in various formats
33+
(e.g., "555-555-5555", "555 555 5555", "(555) 555-5555", "+1 (555) 555-5555") and returns
34+
a normalized string containing only digits.
35+
36+
Args:
37+
phone_number (str): The phone number string to normalize.
38+
39+
Returns:
40+
str: The normalized phone number containing only digits.
41+
Example: "5555555555".
42+
"""
43+
# keep only digits
44+
digits = re.sub(r"\D", "", phone)
45+
46+
# handle North America: allow 10 digits, or 11 with leading "1"
47+
if len(digits) == 11 and digits.startswith("1"):
48+
digits = digits[1:]
49+
else:
50+
return digits

0 commit comments

Comments
 (0)