diff --git a/validate_docbr/BaseDoc.py b/validate_docbr/BaseDoc.py index 58e4ec4..bd437eb 100644 --- a/validate_docbr/BaseDoc.py +++ b/validate_docbr/BaseDoc.py @@ -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) diff --git a/validate_docbr/CNH.py b/validate_docbr/CNH.py index fe500a6..39b23b0 100644 --- a/validate_docbr/CNH.py +++ b/validate_docbr/CNH.py @@ -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): diff --git a/validate_docbr/CNPJ.py b/validate_docbr/CNPJ.py index 033856b..1bac731 100644 --- a/validate_docbr/CNPJ.py +++ b/validate_docbr/CNPJ.py @@ -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: diff --git a/validate_docbr/CNS.py b/validate_docbr/CNS.py index 19080c8..6809867 100644 --- a/validate_docbr/CNS.py +++ b/validate_docbr/CNS.py @@ -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: diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py index 7bfdd42..9ce8e22 100644 --- a/validate_docbr/CPF.py +++ b/validate_docbr/CPF.py @@ -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: @@ -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: @@ -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 diff --git a/validate_docbr/PIS.py b/validate_docbr/PIS.py index ea25658..d896ba1 100644 --- a/validate_docbr/PIS.py +++ b/validate_docbr/PIS.py @@ -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): diff --git a/validate_docbr/TituloEleitoral.py b/validate_docbr/TituloEleitoral.py index 4d9ac82..f7b26c4 100644 --- a/validate_docbr/TituloEleitoral.py +++ b/validate_docbr/TituloEleitoral.py @@ -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: