validateCnpj function exported ✓ 100.0%
Last updated: 2026-02-24T21:07:57.500Z
Location
Metrics
LOC: 20
Complexity: 4
Params: 1
Coverage: 100.0% (8/8 lines, 56x executed)
Signature
validateCnpj(cnpj: string): : boolean
Summary
Validates a Brazilian CNPJ string.
Tags
#@param cnpj - CNPJ string (formatted or raw digits)#@returns `true` if the CNPJ has valid check digits
Source Code
export function validateCnpj(cnpj: string): boolean {
const cleaned = cnpj.replace(/\D/g, "");
if (cleaned.length !== 14) return false;
if (/^(\d)\1{13}$/.test(cleaned)) return false;
const digits = cleaned.split("").map(Number);
const first = calculateCnpjCheckDigit(
digits.slice(0, 12),
[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2],
);
if (first !== digits[12]) return false;
const second = calculateCnpjCheckDigit(
digits.slice(0, 13),
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2],
);
return second === digits[13];
}
Dependencies (Outgoing)
| Target | Type |
|---|---|
| calculateCnpjCheckDigit | calls |
| cleaned | dynamic_call |
No incoming dependencies.