faker-js / faker

Generate massive amounts of fake data in the browser and node.js
https://fakerjs.dev
Other
13.03k stars 919 forks source link

Invalid zip/postal codes when using locale en_CA #1416

Open dbertouille opened 2 years ago

dbertouille commented 2 years ago

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?

Used Package Manager

npm

Shinigami92 commented 2 years ago

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.

dbertouille commented 2 years ago

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`);
  }
}
Shinigami92 commented 2 years ago

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?

dbertouille commented 2 years ago

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

ST-DDT commented 1 month ago

@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:

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 commented 3 weeks ago

I created a PR to fix this issue:

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]")}}');