The cnpjIsValid
function is used to validate a Brazilian CNPJ (National Registry of Legal Entities) number. It returns an object containing the isValid
(boolean) and errorMsg
(string) properties, indicating whether the CNPJ is valid and, in case of an error, the corresponding error message.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { cnpjIsValid } from 'multiform-validator';
The function takes two parameters:
cnpj
(string) - The CNPJ number to be validate.errorMsg
(optional array) - A list of custom error messages. If not provided, it uses a default list of error messages.// Example 1 - Using the CNPJ number '72.501.263/0001-40' const
result1 = cnpjIsValid('72.501.263/0001-40');
console.log(result1.isValid); // true
console.log(result1.errorMsg); // null
// Example 2 - Using the CNPJ number '73.506.263/0001-45' and custom error messages
const result2 = cnpjIsValid('73.506.263/0001-45', ['CNPJ is wrong']);
console.log(result2.isValid); // false
console.log(result2.errorMsg); // 'CNPJ is wrong'
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'.