Skip to content

Add Faker::Company.brazilian_company_alphanumeric - #3307

Open
MoacirPetry wants to merge 1 commit into
faker-ruby:mainfrom
MoacirPetry:brazilian_company_alphanumeric
Open

Add Faker::Company.brazilian_company_alphanumeric#3307
MoacirPetry wants to merge 1 commit into
faker-ruby:mainfrom
MoacirPetry:brazilian_company_alphanumeric

Conversation

@MoacirPetry

@MoacirPetry MoacirPetry commented Jul 27, 2026

Copy link
Copy Markdown

What does this PR do?

Adds Faker::Company.brazilian_company_alphanumeric to generate Brazilian company alphanumeric identification codes (14-character format with two check digits).

The alphanumeric CNPJ was introduced through Normative Instruction issued by the Brazilian Federal Revenue Service (Receita Federal) on October 15, 2024. It regulates how the CNPJ registry operates and establishes the transition to the alphanumeric format starting in July 2026.

Usage

Faker::Company.brazilian_company_alphanumeric #=> "AB12CD34EFGH83"
Faker::Company.brazilian_company_alphanumeric(formatted: true) #=> "AB.12C.D34/EFGH-83"

How it works

  • Generates a random 12-character alphanumeric base string
  • Computes two validation check digits using the official weighted checksum algorithm
  • Returns a 14-character alphanumeric code (base + 2 check digits)
  • Optionally formats with dots and dash matching CNPJ visual style

Additional information

The first alphanumeric CNPJ is scheduled to be issued on July 31, 2026. See the reference over here.
The main goal is simple: prevent the exhaustion of available CNPJ numbers.
Every existing numeric CNPJ remains valid.
Simulations and Validations can be tested out over here.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug, refactor something, or add a feature.
  • Tests and Rubocop are passing before submitting your proposed changes.
  • Added implementation in lib/faker/default/company.rb
  • Added tests validating check digit computation and formatting

If you're proposing a new generator or locale:

  • Double-check the existing generators documentation to make sure the new generator you want to add doesn't already exist.
  • You've reviewed and followed the Contributing guidelines.

@jremes-foss jremes-foss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Thank you for your contribution. The code itself looks good to me, but it looks like Ruby 3.2 and Ruby 3.3 issues are failing due to issue unrelated to your code. I'm looking to fix it as soon as possible to get the pipelines to run successfully.

@MoacirPetry

Copy link
Copy Markdown
Author

It seems that the test fails because "O'reilly's lonely barbtail" has an apostrophe right after the capital O, which doesn't match /[A-Z][a-z]+'s [a-z]+ [a-z]+/ (file: test/faker/creature/test_faker_bird.rb) since it requires lowercase letters between the capital and the apostrophe.

So, change line 89 out there from /[A-Z][a-z]+'s [a-z]+ [a-z]+/, to this one /[A-Z].+'s [a-z]+ [a-z]+/,, should be fix.

Note: Using .+ allows apostrophes within the name before the possessive 's.

@jremes-foss

Copy link
Copy Markdown
Contributor

@MoacirPetry Thank you - I've analysed the situation and came to the same conclusion. I'll implement a fix.

@MoacirPetry

Copy link
Copy Markdown
Author

btw, all O' names like O'connell, O'hara, O'keefe, D'amore have the same class of bug as O'reilly

The .+ doesn't overmatch because the rest of the pattern 's [a-z]+ [a-z]+ constrains it. The .+ always backtracks to find the possessive 's followed by two lowercase words. Other example names like "Baltimore liberal budgerigar" or "Mississippi quizzical nativehen" correctly fall through to pattern 2 /[A-Z][a-z]+ [a-z]+ [a-z]+/

@jremes-foss

jremes-foss commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

The last failure in my pull request seems to be an issue with caching inside ruby/setup-ruby when running against ruby-head. I need to have a look at that later, but the fixed (non-nightly) versions are fine in the pull request branch.

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.

2 participants