Repository: alvarofpp/validate-docbr

Issue:
Complete cpf with zeros
I noticed that if the first digits of a CPF are zeros and they are omitted, the library returns false. I modified it to complete the CPF with zeros before validation. I hope this helps more people.

Hints from issue comments:

Solution patch:
diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py
index 152137f..458458f 100644
--- a/validate_docbr/CPF.py
+++ b/validate_docbr/CPF.py
@@ -18,14 +18,14 @@ class CPF(BaseDoc):
 
         doc = list(self._only_digits(doc))
 
-        if len(doc) != 11:
-            return False
+        if len(doc) < 11:
+            doc = self._complete_with_zeros(doc)
 
         if not self.repeated_digits and self._check_repeated_digits(doc):
             return False
 
         return self._generate_first_digit(doc) == doc[9] \
-               and self._generate_second_digit(doc) == doc[10]
+            and self._generate_second_digit(doc) == doc[10]
 
     def generate(self, mask: bool = False) -> str:
         """Gerar CPF."""
@@ -76,3 +76,8 @@ class CPF(BaseDoc):
         """Verifica se é um CPF com números repetidos.
         Exemplo: 111.111.111-11"""
         return len(set(doc)) == 1
+
+    def _complete_with_zeros(self, doc: str) -> list[str]:
+        """Adiciona zeros à esquerda para completar o CPF."""
+        zeros_needed = 11 - len(doc)
+        return ['0'] * zeros_needed + doc


Test patch:
diff --git a/tests/test_CPF.py b/tests/test_CPF.py
index ffd9455..4707d4d 100644
--- a/tests/test_CPF.py
+++ b/tests/test_CPF.py
@@ -56,3 +56,14 @@ class TestCpf(unittest.TestCase):
         ]
         for cpf, is_valid in cases:
             self.assertEqual(self.cpf.validate(cpf), is_valid)
+
+    def test_add_leading_zeros(self):
+        """Verifica se o método de adicionar zeros à esquerda funciona corretamente."""
+        cases = [
+            ('123456789', False),  # 9 digitos
+            ('12345678901', False),  # 11 digitos
+            ('1234567', False),  # 7 digitos
+            ('9380826044', True) # cpf valido
+        ]
+        for cpf_input, is_valid in cases:
+            self.assertEqual(self.cpf.validate(cpf_input), is_valid)