Skip to content

Commit

Permalink
Merge pull request #34 from alvarofpp/issue-33
Browse files Browse the repository at this point in the history
Resolve #33
  • Loading branch information
alvarofpp authored Oct 23, 2020
2 parents 0dcc748 + 0e4c05a commit 1593259
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 2 deletions.
12 changes: 12 additions & 0 deletions validate_docbr/BaseDoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ def mask(self, doc: str = '') -> str:
def _only_digits(self, doc: str = '') -> str:
"""Remove os outros caracteres que não sejam dígitos."""
return "".join([x for x in doc if x.isdigit()])

def _validate_input(self, input: str, valid_characters: List = None) -> bool:
"""Validar input.
Caso ele possua apenas dígitos e caracteres válidos, retorna True.
Caso possua algum caractere que não seja dígito ou caractere válido, retorna False."""
if valid_characters is None:
valid_characters = ['.', '-', '/', ' ']

set_non_digit_characters = set([x for x in input if not x.isdigit()])
set_valid_characters = set(valid_characters)

return not (len(set_non_digit_characters.difference(set_valid_characters)) > 0)
3 changes: 3 additions & 0 deletions validate_docbr/CNH.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self):

def validate(self, doc: str = '') -> bool:
"""Validar CNH."""
if not self._validate_input(doc, [' ']):
return False

doc = self._only_digits(doc)

if len(doc) != 11 or self._is_repeated_digits(doc):
Expand Down
3 changes: 3 additions & 0 deletions validate_docbr/CNPJ.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def __init__(self):

def validate(self, doc: str = '') -> bool:
"""Validar CNPJ."""
if not self._validate_input(doc, ['.', '/', '-']):
return False

doc = self._only_digits(doc)

if len(doc) != 14:
Expand Down
3 changes: 3 additions & 0 deletions validate_docbr/CNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self):

def validate(self, doc: str = '') -> bool:
"""Validar CNS."""
if not self._validate_input(doc, [' ']):
return False

doc = list(self._only_digits(doc))

if len(doc) != 15 or int(doc[0]) not in self.first_digit:
Expand Down
7 changes: 5 additions & 2 deletions validate_docbr/CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def __init__(self, repeated_digits: bool = False):

def validate(self, doc: str = '') -> bool:
"""Validar CPF."""
if not self._validate_input(doc, ['.', '-']):
return False

doc = list(self._only_digits(doc))

if len(doc) != 11:
Expand All @@ -20,7 +23,7 @@ def validate(self, doc: str = '') -> bool:
if not self.repeated_digits and self._check_repeated_digits(doc):
return False

return self._generate_first_digit(doc) == doc[9]\
return self._generate_first_digit(doc) == doc[9] \
and self._generate_second_digit(doc) == doc[10]

def generate(self, mask: bool = False) -> str:
Expand Down Expand Up @@ -59,7 +62,7 @@ def _generate_second_digit(self, doc: list) -> str:
sum = 0

for i in range(11, 1, -1):
sum += int(doc[11-i]) * i
sum += int(doc[11 - i]) * i

sum = (sum * 10) % 11

Expand Down
3 changes: 3 additions & 0 deletions validate_docbr/PIS.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self):

def validate(self, doc: str = '') -> bool:
"""Validar PIS/NIS/PASEP/NIT."""
if not self._validate_input(doc, ['.', '-']):
return False

doc = self._only_digits(doc)

if len(doc) != 11 or self._is_repeated_digits(doc):
Expand Down
3 changes: 3 additions & 0 deletions validate_docbr/TituloEleitoral.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def __init__(self):

def validate(self, doc: str = '') -> bool:
"""Método para validar o título eleitoral."""
if not self._validate_input(doc, [' ']):
return False

doc_digits = list(map(int, self._only_digits(doc=doc)))

if len(doc_digits) != 12:
Expand Down

0 comments on commit 1593259

Please sign in to comment.