Skip to content

fix: phone numbers from random digits, and DayOfMonth without a space - #106

Merged
bxcodec merged 2 commits into
go-faker:mainfrom
simonkundrik:fix/phone-numbers
Nov 25, 2026
Merged

bxcodec merged 2 commits into
go-faker:mainfrom
simonkundrik:fix/phone-numbers

Conversation

@simonkundrik

Copy link
Copy Markdown
Contributor

I ran every generator through validator.js (isMobilePhone, isEmail, isURL, isIP, isMACAddress, isJWT, isUUID, isISO8601 and so on, 3,000 to 30,000 values each). Two things gave wrong values: the phone numbers and DayOfMonth. The rest was fine.

Phone numbers

Phonenumber, TollFreePhoneNumber and E164PhoneNumber took their digits from RandomInt(1, 10) and RandomInt(1, 9), which return every number of the range once, in a random order. So a phone number was a permutation of 1 to 10 glued together:

  • Phonenumber has only 3.6 million possible numbers (the digits 1 to 9 once each and a 10), 54 of 20,000 came up twice, and 47% do not follow the numbering plan: 102-593-8147 has an area code that starts with 1 (isMobilePhone(x, "en-US") rejects them).
  • TollFreePhoneNumber came out as (777) 296-317548: nine digits after the code where the doc says (888) 937-7238. All of them (20,000 of 20,000) had the wrong length.
  • E164PhoneNumber had 13 digits (+798610152473), the example in the doc has 11 (+27113456789).

The digits are drawn one by one now (randomStringNumber, as ChinesePhoneNumber does):

  • Phonenumber: 201-886-0269. The area code and the exchange start with 2 to 9 and are not N11 (211, 411, 911 and so on are service codes).
  • TollFreePhoneNumber: (888) 937-7238. I kept the 777 and 888 that the tests expect (777 is not a toll free code, the real ones are 800, 833, 844, 855, 866, 877 and 888: say so if you want those).
  • E164PhoneNumber: a plus, the 7 or the 8 that was there and 10 random digits.

DayOfMonth

DayOfMonthFormat is "_2", which pads the days below 10 with a space, so 29% of the values (872 of 3,000) were like " 4". It is "2" now, and time.Parse(DayOfMonthFormat, ...) in the existing tests still works.

How it was tested
  • New tests in phone_test.go: 20,000 Phonenumber against the numbering plan, 20,000 toll free numbers against (888) 937-7238, 20,000 E.164 numbers against a plus and 11 digits, and the digits at the free places of each of them (every digit within 15% of an even share, and few numbers that come up twice). In datetime_test.go: 3,000 days, all 31 of them and no space. All five fail on master (9,423 of 20,000 phone numbers off the plan, 20,000 of 20,000 toll free and E.164 numbers with the wrong length).
  • After the change validator.js accepts all 30,000 Phonenumber values (isMobilePhone, en-US) and all 30,000 are different.
  • go test ./..., go vet and gofmt pass. I could not run -race or golangci-lint here (no C compiler and no linter installed), CI does.

Phonenumber, TollFreePhoneNumber and E164PhoneNumber took their digits from
RandomInt(1, 10) and RandomInt(1, 9), which give every number of the range
once in a random order. So a phone number was a permutation of 1 to 10 (3.6
million possible numbers, the same ten digits in every one) and 47% of them
did not follow the numbering plan: 102-593-8147 has an area code that starts
with 1. A toll free number came out as (777) 296-317548, nine digits where
the doc says (888) 937-7238, and an E.164 number had 13 digits where the doc
has 11.

The digits are drawn one by one now: the area code and the exchange start with
2 to 9 and are not N11, a toll free number is (888) 937-7238 and an E.164
number is a plus and 11 digits.
DayOfMonthFormat was "_2", which pads the days below 10 with a space (" 4"),
so 29% of the values of DayOfMonth had a leading space. It is "2" now: 4.
@bxcodec

bxcodec commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

DayOfMonthFormat is "_2", which pads the days below 10 with a space, so 29% of the values (872 of 3,000) were like " 4". It is "2" now, and time.Parse(DayOfMonthFormat, ...) in the existing tests still works.

Changing this will break the output format, though. Not sure where else it's used. What I'm afraid is that if any service uses it for string comparisons, it will break.

But since the test isn't breaking, it's probably safe.

@bxcodec
bxcodec merged commit c3444af into go-faker:main Nov 25, 2026
2 checks passed
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