Skip to content

Commit

Permalink
fix: return cpfisValid function
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 6, 2024
1 parent 28b6b42 commit a96b97e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
38 changes: 19 additions & 19 deletions packages/python/src/multiform_validator/cpfValidator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Define the default error messages
default_error_msg = [
defaultErrorMsg = [
"CPF invalid",
"CPF must have 11 numerical digits",
"CPF is not valid",
"Unknown error",
]

def cpfValidator(cpf, error_msg=None):
def cpfValidator(cpf, errorMsg=None):
"""
Validates a Brazilian CPF number for correctness.
Expand All @@ -15,50 +15,50 @@ def cpfValidator(cpf, error_msg=None):
validity of a CPF number using its calculation algorithm.
:param cpf: The CPF number as a string.
:param error_msg: An optional list of custom error messages.
:return: A dictionary with 'is_valid' (boolean) and 'error_msg' (string) properties.
:param errorMsg: An optional list of custom error messages.
:return: A dictionary with 'isValid' (boolean) and 'errorMsg' (string) properties.
"""
if error_msg is None:
error_msg = default_error_msg
if errorMsg is None:
errorMsg = defaultErrorMsg
else:
if not isinstance(error_msg, list):
if not isinstance(errorMsg, list):
raise TypeError("Must be a list")
for index, msg in enumerate(error_msg):
for index, msg in enumerate(errorMsg):
if msg is not None and not isinstance(msg, str):
raise TypeError("All values within the list must be strings or None.")

def get_error_message(index):
return error_msg[index] if index < len(error_msg) and error_msg[index] is not None else default_error_msg[index]
return errorMsg[index] if index < len(errorMsg) and errorMsg[index] is not None else defaultErrorMsg[index]

try:
if not cpf:
return {
'is_valid': False,
'error_msg': get_error_message(0),
'isValid': False,
'errorMsg': get_error_message(0),
}

cpf_clean = ''.join(filter(str.isdigit, cpf))

if len(cpf_clean) != 11 or cpf_clean == cpf_clean[0] * 11:
return {
'is_valid': False,
'error_msg': get_error_message(1 if len(cpf_clean) != 11 else 2),
'isValid': False,
'errorMsg': get_error_message(1 if len(cpf_clean) != 11 else 2),
}

def calculate_digit(cpf_slice):
return sum((10 - i) * int(digit) for i, digit in enumerate(cpf_slice)) % 11 % 10

if all(cpf_clean[i] == str(calculate_digit(cpf_clean[:i])) for i in [9, 10]):
return {
'is_valid': True,
'error_msg': None,
'isValid': True,
'errorMsg': None,
}
return {
'is_valid': False,
'error_msg': get_error_message(2),
'isValid': False,
'errorMsg': get_error_message(2),
}
except Exception as e:
return {
'is_valid': False,
'error_msg': get_error_message(3),
'isValid': False,
'errorMsg': get_error_message(3),
}
3 changes: 3 additions & 0 deletions packages/python/tests/general.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def tester():
phoneTrueValue2 = validateBRPhoneNumber('(11) 98765-4321')
phoneFalseValue = validateBRPhoneNumber('(11) 1111-111')

print(cpfTrueValue)
print(cpfFalseValue)

successTests = []
failedTests = []

Expand Down

0 comments on commit a96b97e

Please sign in to comment.