Skip to content

Commit

Permalink
Adding phone number valdator to member model
Browse files Browse the repository at this point in the history
  • Loading branch information
b-d055 committed Jun 20, 2024
1 parent 649aae5 commit ba6994a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/meshapi/models/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.db.models.fields import EmailField
from django_jsonform.models.fields import ArrayField as JSONFormArrayField

from meshapi.validation import validate_phone_number_field


class Member(models.Model):
name = models.CharField(help_text='Member full name in the format: "First Last"')
Expand All @@ -22,7 +24,7 @@ class Member(models.Model):
help_text="Any additional email addresses associated with this member",
)
phone_number = models.CharField(
default=None, blank=True, null=True, help_text="A contact phone number for this member"
default=None, blank=True, null=True, help_text="A contact phone number for this member", validators=[validate_phone_number_field]
)
slack_handle = models.CharField(default=None, blank=True, null=True, help_text="The member's slack handle")
notes = models.TextField(
Expand Down
5 changes: 5 additions & 0 deletions src/meshapi/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from dataclasses import dataclass

from django.core.exceptions import ValidationError

import phonenumbers
import requests
from validate_email import validate_email
Expand Down Expand Up @@ -33,6 +35,9 @@ def validate_phone_number(phone_number: str) -> bool:
return False
return True

def validate_phone_number_field(phone_number: str):
if not validate_phone_number(phone_number):
raise ValidationError(f"Invalid phone number: {phone_number}")

# Used to obtain info about addresses within NYC. Uses a pair of APIs
# hosted by the city with all kinds of good info. Unfortunately, there's
Expand Down

0 comments on commit ba6994a

Please sign in to comment.