diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3f..287d7602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `is_valid_phone` aceitava dígitos de outros alfabetos, como `٩` [#807](https://github.com/brazilian-utils/python/issues/807) - Cálculo do dígito verificador em `is_valid_legal_process` e `generate_legal_process`, que rejeitava números de processo reais e gerava números inválidos [#785](https://github.com/brazilian-utils/python/pull/785) ## [2.5.0] - 2026-06-30 diff --git a/README.md b/README.md index 5ba23fd5..fa5722de 100644 --- a/README.md +++ b/README.md @@ -510,7 +510,7 @@ Argumentos: - phone_number (str): - o número de telefone a ser validado - - apenas dígitos, sem símbolos + - apenas dígitos de 0 a 9, sem símbolos - sem o código do país - deve incluir o número de DDD com dois dígitos - exemplo: '+55 48 9999 9999' deve ser utilizado como '4899999999' diff --git a/README_EN.md b/README_EN.md index 0b9167a8..4495c8a0 100644 --- a/README_EN.md +++ b/README_EN.md @@ -530,7 +530,7 @@ Args: - phone_number: - the phone number to be validated - - digits only, no symbols + - ASCII digits (0-9) only, no symbols - without the country code - should include the area code (DDD) with two digits - example: '+55 48 9999 9999' should be used as '4899999999' diff --git a/brutils/phone.py b/brutils/phone.py index 46b063df..4b84b070 100644 --- a/brutils/phone.py +++ b/brutils/phone.py @@ -41,7 +41,7 @@ def is_valid(phone_number: str, type: str = None) -> bool: Args: phone_number (str): The phone number to validate. - Only digits, without country code. + Only ASCII digits (0-9), without country code. It should include two digits DDD. type (str): "mobile" or "landline". If not specified, checks for one or another. @@ -148,14 +148,14 @@ def _is_valid_mobile(phone_number: str) -> bool: Args: phone_number (str): The mobile number to validate. - Only digits, without country code. + Only ASCII digits (0-9), without country code. It should include two digits DDD. Returns: bool: True if the phone number is valid. False otherwise. """ - pattern = re.compile(r"^[1-9][1-9][9]\d{8}$") + pattern = re.compile(r"^[1-9][1-9][9]\d{8}$", re.ASCII) return ( isinstance(phone_number, str) and re.match(pattern, phone_number) is not None @@ -169,14 +169,14 @@ def _is_valid_landline(phone_number: str) -> bool: Args: phone_number (str): The landline number to validate. - Only digits, without country code. + Only ASCII digits (0-9), without country code. It should include two digits DDD. Returns: bool: True if the phone number is valid. False otherwise. """ - pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$") + pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$", re.ASCII) return ( isinstance(phone_number, str) and re.match(pattern, phone_number) is not None diff --git a/tests/phone/test_phone.py b/tests/phone/test_phone.py index c1ccd762..ed72a46c 100644 --- a/tests/phone/test_phone.py +++ b/tests/phone/test_phone.py @@ -12,6 +12,25 @@ class TestPhone(TestCase): + def test_is_valid(self): + # When the phone number is valid + self.assertIs(is_valid("11994029275"), True) + self.assertIs(is_valid("11994029275", type="mobile"), True) + self.assertIs(is_valid("1938814933"), True) + self.assertIs(is_valid("1938814933", type="landline"), True) + + # When the phone number contains non-ASCII digits, returns False + self.assertIs(is_valid("119" + "٩" * 8), False) + self.assertIs(is_valid("119" + "٩" * 8, type="mobile"), False) + self.assertIs(is_valid("113" + "٩" * 7), False) + self.assertIs(is_valid("113" + "٩" * 7, type="landline"), False) + self.assertIs(is_valid("11994029275"), False) + + # When the phone number contains superscript digits, returns False + self.assertIs(is_valid("1199402927" + "⁵"), False) + self.assertIs(is_valid("1199402927" + "⁵", type="mobile"), False) + self.assertIs(is_valid("193881493" + "³", type="landline"), False) + def test_remove_symbols_phone(self): # When the string empty, it returns an empty string self.assertEqual(remove_symbols_phone(""), "")