cpfIsValid Function Documentation

The cpfIsValid function is used to validate a Brazilian CPF (Individual Taxpayer Identification) number. It returns an object containing the isValid (boolean) and errorMsg (string) properties, indicating whether the CPF is valid and, in case of an error, the corresponding error message.

Import

There are two ways to import the function:

Parameters

The function takes two parameters:

Examples

// Example 1 - Using the CPF number '123.456.789.10'
const result1 = cpfIsValid('123.456.789.10');
console.log(result1.isValid); // false
console.log(result1.errorMsg); // 'CPF invalid'

// Example 2 - Using the CPF number '12345678910' and custom error messages
const result2 = cpfIsValid("12345678910", [
	"CPF is wrong",
	"Must have at least 11 digits",
]);
console.log(result2.isValid); // false
console.log(result2.errorMsg); // 'CPF is wrong'

// Example 3 - Using a valid CPF number '52998224725'
const result3 = cpfIsValid('52998224725');
console.log(result3.isValid); // true
console.log(result3.errorMsg); // null

Notes

If the function is called with an invalid value for the errorMsg parameter (non-array), or if an error occurs during the validation process, the function will return an object with isValid set to false and errorMsg containing the default error message 'Unknown error'.