Skip to content

fix(locale): use only valid letters in en_CA postcodes - #3951

Open
MahinAnowar wants to merge 2 commits into
faker-js:nextfrom
MahinAnowar:fix/en-ca-postcode-letters
Open

fix(locale): use only valid letters in en_CA postcodes#3951
MahinAnowar wants to merge 2 commits into
faker-js:nextfrom
MahinAnowar:fix/en-ca-postcode-letters

Conversation

@MahinAnowar

Copy link
Copy Markdown
Contributor

Fixes #1416

The en_CA postcode patterns use ? for the second and third letters, and replaceSymbols expands ? to any letter A-Z. Canadian postal codes never use D, F, I, O, Q or U though, so roughly 41% of generated codes are invalid — I measured 8,195 bad out of 20,000 draws on the current release, which lines up with the theoretical 1-(20/26)² for two unconstrained letter positions. The first letter was already restricted to the valid 18, which is why only the other two leak.

The locale already knows the right answer elsewhere: postcode_by_state.ts uses fromRegExp with [ABCEGHJ-NPRSTVW-Z], so zipCode({ state: 'ON' }) has always been correct while plain zipCode() isn't.

So this multiplies the patterns out, which is what you suggested in the thread ("You might have to duplicate some lines... Maybe multiply it out fully"). It's built programmatically like the nl fix in #3888 rather than being a literal 7,200-line list — 18 first letters × 20 × 20. Same shape as #3905 for fr, so this stays locale-data only and doesn't touch replaceSymbols or the v11 resolver plans.

The letters list also gets W and Z back for the non-first positions, which is why the locale-data character-inventory snapshot picks up a Z and drops the ?.

Added a test next to the nl/fr ones asserting 1,000 draws all match /^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] \d[ABCEGHJ-NPRSTV-Z]\d$/ — it fails on next and passes here. Full suite is green after a build (53,135 tests), and against the built bundle 20,000 draws now give 0 invalid.

One note: I left the existing returns zipCode with proper locale format assertion alone even though its [A-Za-z] classes are loose enough that they never caught this — happy to tighten it if you'd like.

@MahinAnowar
MahinAnowar requested a review from a team as a code owner July 15, 2026 05:36
@netlify

netlify Bot commented Jul 15, 2026

Copy link
Copy Markdown

Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6b7b59c
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/6a67c50359607700082efaf9
😎 Deploy Preview https://deploy-preview-3951.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@ST-DDT ST-DDT added p: 1-normal Nothing urgent c: locale Permutes locale definitions m: location Something is referring to the location module labels Jul 27, 2026
@ST-DDT ST-DDT added this to the v10.x milestone Jul 27, 2026
@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.91%. Comparing base (717fa14) to head (6b7b59c).

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #3951   +/-   ##
=======================================
  Coverage   98.91%   98.91%           
=======================================
  Files         924      925    +1     
  Lines        3224     3230    +6     
  Branches      585      567   -18     
=======================================
+ Hits         3189     3195    +6     
  Misses         31       31           
  Partials        4        4           
Files with missing lines Coverage Δ
src/locales/en_CA/location/postcode.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

c: locale Permutes locale definitions m: location Something is referring to the location module p: 1-normal Nothing urgent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid zip/postal codes when using locale en_CA

2 participants