diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3..93ecff1 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_cep` e `format_cep` aceitavam dígitos não ASCII, como `²` [#805](https://github.com/brazilian-utils/python/pull/805) - 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 5ba23fd..2a1463d 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,7 @@ Exemplo: Verifica se um CEP (Código de Endereçamento Postal) brasileiro é válido. Para que um CEP seja considerado válido, a entrada deve ser uma string contendo -exatamente 8 dígitos. Esta função não verifica se o CEP é um CEP real, pois +exatamente 8 dígitos de 0 a 9. Esta função não verifica se o CEP é um CEP real, pois valida apenas o formato da string. Argumentos: diff --git a/README_EN.md b/README_EN.md index 0b9167a..b45cbda 100644 --- a/README_EN.md +++ b/README_EN.md @@ -330,7 +330,7 @@ Example: ### is_valid_cep Checks if a Brazilian CEP (Postal Code) is valid. -To be considered valid, the input must be a string containing exactly 8 digits. +To be considered valid, the input must be a string containing exactly 8 digits (0-9). This function does not verify if the CEP is a real postal code; it only validates the format of the string. diff --git a/brutils/cep.py b/brutils/cep.py index 46ef975..6988389 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -67,7 +67,7 @@ def is_valid(cep: str) -> bool: Checks if a CEP (Postal Code) is valid. To be considered valid, the input must be a string containing exactly 8 - digits. + ASCII digits (0-9). This function does not verify if the CEP is a real postal code; it only validates the format of the string. @@ -89,7 +89,12 @@ def is_valid(cep: str) -> bool: https://en.wikipedia.org/wiki/Código_de_Endereçamento_Postal """ - return isinstance(cep, str) and len(cep) == 8 and cep.isdigit() + return ( + isinstance(cep, str) + and len(cep) == 8 + and cep.isascii() + and cep.isdigit() + ) def generate() -> str: diff --git a/tests/test_cep.py b/tests/test_cep.py index e47b12d..c8f1afe 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -34,6 +34,11 @@ def test_is_valid(self): # When CEP does not contain only digits, returns False self.assertIs(is_valid("1234567-"), False) + # When CEP contains non-ASCII digits, returns False + self.assertIs(is_valid("²" * 8), False) + self.assertIs(is_valid("01310200"), False) + self.assertIs(is_valid("٠١٣١٠٢٠٠"), False) + # When CEP is valid self.assertIs(is_valid("99999999"), True) self.assertIs(is_valid("88390000"), True)