From c900c8589a6816bbb925a8da961284bd4a4faf2c Mon Sep 17 00:00:00 2001 From: vasileios Date: Wed, 28 Jun 2023 08:54:09 +0200 Subject: [PATCH] [#1558] Fixed validator according to PR comments --- .../utils/tests/test_validators.py | 19 ++++++++++---- src/open_inwoner/utils/validators.py | 26 +++++++++---------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/open_inwoner/utils/tests/test_validators.py b/src/open_inwoner/utils/tests/test_validators.py index 2c7ec1319a..2ec6798b70 100644 --- a/src/open_inwoner/utils/tests/test_validators.py +++ b/src/open_inwoner/utils/tests/test_validators.py @@ -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( @@ -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, + ) diff --git a/src/open_inwoner/utils/validators.py b/src/open_inwoner/utils/validators.py index a31f574b29..e0e32b8bab 100644 --- a/src/open_inwoner/utils/validators.py +++ b/src/open_inwoner/utils/validators.py @@ -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) @@ -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