faker icon indicating copy to clipboard operation
faker copied to clipboard

Invalid zip/postal codes when using locale en_CA

Open dbertouille opened this issue 3 years ago • 6 comments

Pre-Checks

Describe the bug

When generating zip codes (postal codes) using the en_CA locale, invalid codes are generated. The characters D, F, I, O, Q, or U are not used in postal codes. This is can cause the randomly generated codes to fail validation.

Reference Documentation

Minimal reproduction code

const { faker } = require('@faker-js/faker');

faker.locale = 'en_CA';

console.log(faker.address.zipCode());

Additional Context

No response

Environment Info

System:
    OS: macOS 12.3
    CPU: (8) arm64 Apple M1 Pro
    Memory: 96.56 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.15.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 106.1.44.101
    Chrome: 105.0.5195.52
    Safari: 15.4
  npmPackages:
    @faker-js/faker: ^7.5.0 => 7.5.0

Which module system do you use?

  • [ ] CJS
  • [ ] ESM

Used Package Manager

npm

dbertouille avatar Oct 07 '22 13:10 dbertouille

Oh interesting, I assume you don't mean the first letter but following letters?

https://github.com/faker-js/faker/blob/7327e6e6fd4061f10647a5fabf437406c9ee8066/src/locales/en_CA/address/postcode.ts

We don't have a strategy yet for post validating content generated by pattern :thinking:

Could you provide some invalid generated postcodes? We can use these e.g. for negative tests.

Shinigami92 avatar Oct 07 '22 13:10 Shinigami92

For sure. Here are some examples.

P6V 1E1 is valid
V9O 9X4 is invalid
P7F 8L6 is invalid
G6O 7V0 is invalid
L6D 1K9 is invalid
E0T 2W7 is valid
G3L 4T8 is valid
E3P 6X3 is valid
J9A 4E7 is valid
N1Y 4H9 is valid

The following script can be used to generate more examples.

const { faker } = require('@faker-js/faker');

faker.locale = 'en_CA';

for (let i = 0; i < 10; i++) {
  const code = faker.address.zipCode();

  if (code.match(/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/)) {
    console.log(`${code} is valid`);
  } else {
    console.log(`${code} is invalid`);
  }
}

dbertouille avatar Oct 07 '22 14:10 dbertouille

Our internal resources are currently filled with preparing the move to start with v8, So I'm sorry if we cannot handle a fix right now. Also we might think about a robust strategy to handle this bug in a higher order.

Until then, would it be okay to just use a loop until a valid postcode was generated?

Shinigami92 avatar Oct 07 '22 14:10 Shinigami92

No problem, I can work around it. Thanks for the quick response!

dbertouille avatar Oct 07 '22 14:10 dbertouille

@dbertouille Sorry, we kind of forgot about this issue.

Are you willing to create a PR for this now? If yes, please check and update the pattern here:

  • https://github.com/faker-js/faker/blob/next/src/locales/en_CA/location/postcode.ts

You might have to duplicate some lines

e.g.

'A#? #?#' -> 'A#A #E#' + 'A#F #X#' + ...

Maybe multiply it out fully and then pick 100 random patterns?

ST-DDT avatar Oct 20 '24 19:10 ST-DDT

I created a PR to fix this issue:

  • #3235

Until the PR is merged, you can use the following workaround:

const zipCode = faker.helpers.fake('{{helpers.fromRegExp("[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] [0-9][ABCEGHJ-NPRSTV-Z][0-9]")}}');

ST-DDT avatar Oct 29 '24 22:10 ST-DDT