Skip to content

feat: add support for alphanumeric CNPJ format#62

Closed
JeanNesi wants to merge 1 commit into
reactivando:mainfrom
JeanNesi:feature/cnpj-alphanumeric-support
Closed

feat: add support for alphanumeric CNPJ format#62
JeanNesi wants to merge 1 commit into
reactivando:mainfrom
JeanNesi:feature/cnpj-alphanumeric-support

Conversation

@JeanNesi

@JeanNesi JeanNesi commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

What changes

Adds support for the new alphanumeric CNPJ format (12 uppercase alphanumeric positions + 2 numeric check digits), as defined by the Receita Federal, while keeping the traditional numeric format fully functional.

Examples now accepted:

  • 12.ABC.345/01DE-35 (with punctuation)
  • 12ABC34501DE35 (without punctuation)
  • 00.000.000/0000-00 / 00000000000000 (legacy numeric format)

How

  • The check digit calculation now converts each character via its ASCII value offset by 48 ('0' → 0, 'A' → 17), following the Receita Federal spec.
  • Validation logic was extracted into two helpers: charToNumber and generateChecksum.
  • Normalization now strips only the separators (., /, -), and validation uses the regex ^[A-Z\d]{12}\d{2}$, ensuring uppercase letters in the first 12 positions and numeric digits in the 2 check positions.

Tests

Coverage added in validateCNPJ.spec.ts and usevalidationsbr.spec.ts:

  • Valid and invalid alphanumeric CNPJs
  • Repeated characters (e.g. BB.BBB.BBB/BBBB-BB, AAAAAAAAAAAAAA)
  • Incorrect lengths (shorter and longer than expected)
  • Lowercase letters and non-numeric check digits being rejected

All tests pass with pnpm test.

@Guiroos

Guiroos commented Jun 9, 2026

Copy link
Copy Markdown

Nice implementation of the alphanumeric support!
Followed the patterns of the project and did a good job with the description. Hope that this will be approved 🙏🏻

@arielff3 arielff3 requested a review from AZagatti June 9, 2026 21:45
AZagatti added a commit that referenced this pull request Jun 24, 2026
Adds alphanumeric CNPJ validation (12 alphanumeric + 2 numeric DV) per the Receita Federal spec, keeping legacy numeric support. Consolidates #59/#60/#62; base implementation from #62 (@JeanNesi). Full canonical test set; 115 tests pass.

Co-authored-by: Jean Carlos Nesi <jean.nesi@nextfit.com.br>
@AZagatti

Copy link
Copy Markdown
Collaborator

Thank you very much for this, @JeanNesi! 🙏

I reviewed all three alphanumeric-CNPJ PRs against the official Receita Federal reference implementation, and yours was selected as the base: it was the only one using the correct ASCII offset of 48 ('0'→0, 'A'→17). The clean charToNumber / generateChecksum split also fit the codebase nicely (it mirrors validatePIS).

The main branch moved a lot since you opened this (migration to pnpm 11, Biome/ultracite, dependency updates), so rather than merge this branch directly I consolidated your implementation into a fresh PR on top of the current main, adapted it to Biome, and added the full canonical test set from the official reference (including the discriminating 00000000000191 → valid case).

Merged in #67 with you as co-author. Closing this in favor of that PR — thanks again for the great work!

@AZagatti

Copy link
Copy Markdown
Collaborator

Consolidated and merged via #67 (you are credited as co-author). Thank you!

@AZagatti AZagatti closed this Jun 24, 2026
AZagatti added a commit that referenced this pull request Jun 24, 2026
Adds alphanumeric CNPJ validation (12 alphanumeric + 2 numeric DV) per the
Receita Federal spec, keeping legacy numeric support. Consolidates
#59/#60/#62; base implementation from #62 (@JeanNesi). Full canonical test
set; 115 tests pass.

Co-authored-by: Jean Carlos Nesi <102368879+JeanNesi@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JeanNesi

Copy link
Copy Markdown
Contributor Author

Thank you for the detailed review and for carrying this forward! I'm glad the implementation was useful and that it could serve as the basis for the final solution. I really appreciate the co-author credit and the effort to adapt everything to the current codebase. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants