Skip to content

Commit

Permalink
[#1558] Fixed validator according to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Jun 28, 2023
1 parent 1637e69 commit c900c85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
19 changes: 14 additions & 5 deletions src/open_inwoner/utils/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ def test_dutch_phonenumber_validator(self):
"1123456789",
"+31123456789",
"0123456789",
"012-3456789",
"012 345 67 89",
"+31 12 345 67 89",
"0695azerty",
"azerty0545",
"@4566544++8",
"onetwothreefour",
]
invalid_samples_2 = [
"012-3456789",
"012 345 67 89",
"+31 12 345 67 89",
]

for valid_num in valid_samples:
errors = DutchPhoneNumberValidator()(valid_num)
self.assertIsNone(errors)
self.assertIsNone(DutchPhoneNumberValidator()(valid_num))

for invalid_num in invalid_samples:
self.assertRaisesMessage(
Expand All @@ -124,3 +125,11 @@ def test_dutch_phonenumber_validator(self):
DutchPhoneNumberValidator(),
invalid_num,
)

for invalid_num in invalid_samples_2:
self.assertRaisesMessage(
ValidationError,
_("The phone number cannot contain spaces or dashes"),
DutchPhoneNumberValidator(),
invalid_num,
)
26 changes: 12 additions & 14 deletions src/open_inwoner/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def __call__(self, value: str) -> "PhoneNumber":

@deconstructible
class DutchPhoneNumberValidator:
country = "NL"
country_name = _("Dutch")
language = "NL"
country_name = _("Netherlands")
error_message = _(
"Not a valid dutch phone number. An example of a valid dutch phone number is 0612345678"
)
_parse_phonenumber: ParsePhoneNumber

def __call__(self, value):
self.check_for_invalid_chars(value)
self._check_for_invalid_chars(value)

parsed_value = self._parse_phonenumber(value)

Expand All @@ -45,30 +45,28 @@ def __call__(self, value):
# this additional check is needed because while .parse() does some checks on country
# is_possible_number() and is_valid_number() do not
# eg: country=NL would accept "+442083661177"
if self.country:
if not phonenumbers.is_valid_number_for_region(parsed_value, self.country):
raise ValidationError(
self.error_message,
params={"country": self.country_name},
code="invalid",
)
if not phonenumbers.is_valid_number_for_region(parsed_value, self.language):
raise ValidationError(
self.error_message,
params={"country": self.country_name},
code="invalid",
)

def _parse_phonenumber(self, value: str) -> "PhoneNumber":
try:
return phonenumbers.parse(value, self.country)
return phonenumbers.parse(value, self.language)
except phonenumbers.NumberParseException:
raise ValidationError(
self.error_message,
code="invalid",
)

def check_for_invalid_chars(self, value):
def _check_for_invalid_chars(self, value: str) -> None:
if " " in value or "-" in value:
raise ValidationError(
self.error_message,
_("The phone number cannot contain spaces or dashes"),
code="invalid",
)
return value


@deconstructible
Expand Down

0 comments on commit c900c85

Please sign in to comment.