Skip to content

Add alphanumeric CNPJ support - #115

Merged
pnzrfst merged 8 commits into
masterfrom
feature/alphanumeric-cnpj
Apr 9, 2026
Merged

Add alphanumeric CNPJ support#115
pnzrfst merged 8 commits into
masterfrom
feature/alphanumeric-cnpj

Conversation

@pnzrfst

@pnzrfst pnzrfst commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Update format_regex in Cnpj to accept uppercase alphanumeric characters ([A-Z0-9]) in the first 12 positions, keeping check digits as numeric-only
  • Update format_number in Cnpj to use number_without_mask with an alphanumeric-aware regex
  • Update reduce_weights in Mod11 to use char.ord - 48 instead of char.to_i, enabling correct weight calculation for letter characters — backwards-compatible for CPF since digits produce identical results under both approaches

Test plan

  • Valid alphanumeric CNPJ passes (unformatted and formatted)
  • Invalid alphanumeric CNPJ with wrong check digit is rejected
  • Lowercase alphanumeric input is rejected
  • All existing numeric CNPJ and CPF specs continue to pass

🤖 Generated with Claude Code

Update CNPJ validation and formatting to accept the new alphanumeric
format introduced by Receita Federal, where the first 12 characters
may contain uppercase letters (A-Z) in addition to digits.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@qltysh

qltysh Bot commented Apr 8, 2026

Copy link
Copy Markdown

Qlty

Coverage Impact

This PR will not change total coverage.

Modified Files with Diff Coverage (4)

RatingFile% DiffUncovered Line #s
Coverage rating: A Coverage rating: A
lib/br_documents/cnpj_cpf/document.rb100.0%
Coverage rating: A Coverage rating: A
lib/br_documents/cnpj_cpf/cnpj.rb100.0%
Coverage rating: A Coverage rating: A
lib/br_documents/suframa.rb100.0%
Coverage rating: A Coverage rating: A
lib/br_documents/commons/mod11.rb100.0%
Total100.0%
🚦 See full report on Qlty Cloud »

🛟 Help
  • Diff Coverage: Coverage for added or modified lines of code (excludes deleted files). Learn more.

  • Total Coverage: Coverage for the whole repository, calculated as the sum of all File Coverage. Learn more.

  • File Coverage: Covered Lines divided by Covered Lines plus Missed Lines. (Excludes non-executable lines including blank lines and comments.)

    • Indirect Changes: Changes to File Coverage for files that were not modified in this PR. Learn more.

Comment thread lib/br_documents/cnpj_cpf/cnpj.rb Outdated
Comment thread spec/br_documents/cnpj_cpf/cnpj_spec.rb
pnzrfst and others added 2 commits April 8, 2026 11:47
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@pnzrfst
pnzrfst merged commit dd9efb0 into master Apr 9, 2026
5 checks passed
@pnzrfst
pnzrfst deleted the feature/alphanumeric-cnpj branch April 9, 2026 12:59
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