-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
More file actions
92 lines (74 loc) · 2.44 KB
/
cpfValidator.py
File metadata and controls
92 lines (74 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
defaultErrorMsg = [
'CPF invalid',
'CPF must have 11 numerical digits',
'CPF is not valid',
'Unknown error',
]
def cpfValidator(cpf, errorMsg = defaultErrorMsg):
"""
Validates a CPF (Brazilian individual taxpayer registry identification) number.
:param cpf: The CPF number to be validated.
:param error_msg: An optional list of error messages.
:return: A dictionary with 'is_valid' (boolean) and 'error_msg' (string) properties.
"""
if (type(cpf) != str): raise TypeError('The input should be a string.')
if (errorMsg):
if (not isinstance(errorMsg, list)):
raise TypeError('Must be a list')
for index in range(len(errorMsg)):
if errorMsg[index] is not None and not isinstance(errorMsg[index], str):
raise TypeError('All values within the list must be strings or None.')
def getErrorMessage(index):
if (errorMsg and index >= 0 and index < len(errorMsg)):
errorMessage = errorMsg[index]
return errorMessage if errorMessage is not None else defaultErrorMsg[index]
return defaultErrorMsg[index]
try:
if(not cpf):
return {
"isValid": False,
"errorMsg": getErrorMessage(0)
}
numeroBase = 10
numeroBase2 = 11
somaTotal = somaTotal2 = 0
if(len(cpf) != 11 and len(cpf) != 14):
return {
"isValid": False,
"errorMsg": getErrorMessage(1),
}
cpfLimpo = ''.join(filter(str.isdigit, cpf))
if (len(cpfLimpo) == 11 and cpfLimpo == cpfLimpo[0] * 11):
return {
"isValid": False,
"errorMsg": getErrorMessage(2),
}
primeiroVerificador = segundoVerificador = repetidor = 0
while (repetidor < 11):
multiplicador = int(cpfLimpo[repetidor]) * numeroBase
numeroBase -= 1
somaTotal += multiplicador
multiplicador2 = int(cpfLimpo[repetidor]) * numeroBase2
numeroBase2 -= 1
somaTotal2 += multiplicador2
valorDeVerificacao = somaTotal - int(cpfLimpo[9])
valorDeVerificacao2 = somaTotal2 - int(cpfLimpo[10])
primeiroVerificador = 11 - (valorDeVerificacao % 11)
segundoVerificador = 11 - (valorDeVerificacao2 % 11)
repetidor += 1
if(primeiroVerificador > 9): primeiroVerificador = 0
if(segundoVerificador > 9): segundoVerificador = 0
if(primeiroVerificador == int(cpfLimpo[9]) and segundoVerificador == int(cpfLimpo[10])):
return {
"isValid": True,
"errorMsg": None,
}
return {
"isValid": False,
"errorMsg": getErrorMessage(2)
}
except:
return {
"isValid": False,
"errorMsg": getErrorMessage(3),
}