validateCnpj function exported ✓ 100.0%

Last updated: 2026-02-24T21:07:57.500Z

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)

graph LR validateCnpj["validateCnpj"] calculateCnpjCheckDigit["calculateCnpjCheckDigit"] validateCnpj -->|calls| calculateCnpjCheckDigit style validateCnpj fill:#dbeafe,stroke:#2563eb,stroke-width:2px click validateCnpj "f1ee8ab875fcae83.html" click calculateCnpjCheckDigit "c26a9642e8863d68.html"
TargetType
calculateCnpjCheckDigit calls
cleaned dynamic_call

No incoming dependencies.