-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Clear and concise description of the problem
Relevant "Discussions" thread: #3532
Faker currently generates en_US
state zipcodes using integer ranges defined in /locales/en_US/location/postcode_by_state.ts, like so:
export default {
AK: '{{number.int({"min": 99501,"max": 99950})}}',
AL: '{{number.int({"min": 35004,"max": 36925})}}',
AR: [
'{{number.int({"min": 71601,"max": 72642})}}',
'{{number.int({"min": 72644,"max": 72959})}}',
],
AZ: '{{number.int({"min": 85001,"max": 86556})}}',
CA: '{{number.int({"min": 90001,"max": 96162})}}',
CO: '{{number.int({"min": 80001,"max": 81658})}}',
CT: '0{{number.int({"min": 6001,"max": 6389})}}',
However, some of the values it generates (e.g. 02803
) are simply random 5-digit integers; the United States Postal Service (USPS) doesn't recognize them:

I believe a significant percentage of the US zipcodes generated by Faker are invalid for this reason. For example, here is the single line of code Faker currently uses to generate Rhode Island (RI
) zip codes:
RI: '0{{number.int({"min": 2801,"max": 2940})}},'
However, of those 140 zipcodes, 55 of them (39%) are invalid & not recognized by the USPS:
02803,02805,02810,02811,02819,02820,02821,02834,02836,02843,02844,02845,02846,02847,02848,02849,02850,02851,02853,02854,02855,02856,02858,02866,02867,02868,02869,02870,02884,02890,02897,02899,02900,02902,02912,02913,02918,02922,02923,02924,02925,02926,02927,02928,02929,02930,02931,02932,02933,02934,02935,02936,02937,02938,02939
Suggested solution
Here is a snippet of an updated set of code that describes only the 85 Rhode Island (RI
) zipcodes recognized by the USPS:
RI: [
'0{{number.int({"min": 2801,"max": 2802})}}',
'02804',
'0{{number.int({"min": 2806,"max": 2809})}}',
'0{{number.int({"min": 2812,"max": 2818})}}',
'0{{number.int({"min": 2822,"max": 2833})}}',
'0{{number.int({"min": 2835,"max": 2842})}}',
'02852',
'0{{number.int({"min": 2857,"max": 2865})}}',
'0{{number.int({"min": 2871,"max": 2883})}}',
'0{{number.int({"min": 2885,"max": 2889})}}',
'0{{number.int({"min": 2891,"max": 2896})}}',
'02898',
'0{{number.int({"min": 2901,"max": 2912})}}',
'0{{number.int({"min": 2914,"max": 2921})}}',
],
In cases where only a single zipcode is in a "range", I forego use of the number.int()
function & use a plain ol' string literal.
Alternative
No response
Additional context
No response