Skip to content

fix(iban): more strict pattern for IE and PS #3464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2025

Conversation

xDivisionByZerox
Copy link
Member

@xDivisionByZerox xDivisionByZerox commented Apr 2, 2025

Description

This PR fixes #3463.

How to Test

To verify the change you can open the deploy review of the docs and past the following in the dev console:

// load faker
await enableFaker();

// load validator's isIban function
const { default: isIban } = await import('https://unpkg.com/[email protected]/es/lib/isIBAN.js');

// define testing function
function testRandomIban() {
  for (const localeCode of ['IE', 'PS']) {
    for (const iban of faker.helpers.multiple(() => faker.finance.iban({ countryCode: localeCode }), { count: 100 })) {
      if(!isIban(iban)) {
        throw new Error('Found IBAN thats still invalid: ' + iban);
      }
    }
  }

  console.log('Yey! All generated ibans were valid :)')
}

// run the test once
testRandomIban();

The expected output is the log that tells you that all ibans were valid. If you want to repeat the test (with newly generated ibans) you can simply call testRandomIban() in the dev console again.

@xDivisionByZerox xDivisionByZerox added c: bug Something isn't working p: 1-normal Nothing urgent m: finance Something is referring to the finance module labels Apr 2, 2025
@xDivisionByZerox xDivisionByZerox requested a review from a team April 2, 2025 15:20
@xDivisionByZerox xDivisionByZerox self-assigned this Apr 2, 2025
@xDivisionByZerox xDivisionByZerox requested a review from a team as a code owner April 2, 2025 15:20
Copy link

netlify bot commented Apr 2, 2025

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit 68462aa
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/67ed55b1042f0a0008d3ad20
😎 Deploy Preview https://deploy-preview-3464.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@xDivisionByZerox xDivisionByZerox modified the milestones: vAnytime, v9.x Apr 2, 2025
Copy link

codecov bot commented Apr 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.97%. Comparing base (048c325) to head (68462aa).
Report is 1 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #3464   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files        2819     2819           
  Lines      217500   217500           
  Branches      953      952    -1     
=======================================
  Hits       217447   217447           
  Misses         53       53           
Files with missing lines Coverage Δ
src/modules/finance/iban.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.

Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR changes the used algorithm from type c to a which then does not generate digits anymore

if (bban.type === 'a') {
s += this.faker.helpers.arrayElement(iban.alpha);
} else if (bban.type === 'c') {
if (this.faker.datatype.boolean(0.8)) {
s += this.faker.number.int(9);
} else {
s += this.faker.helpers.arrayElement(iban.alpha);
}
} else {

@xDivisionByZerox
Copy link
Member Author

This PR changes the used algorithm from type c to a which then does not generate digits anymore

I guess you answered that because the implementation wasn't that clear? At least, that's what I was thinking when starting to work on the issue. I'm certain we could update this code (in a separate PR) to make it more readable. I'm also not sure if we need all featured that the Iban data structure provides 🤷

@Shinigami92
Copy link
Member

This PR changes the used algorithm from type c to a which then does not generate digits anymore

I guess you answered that because the implementation wasn't that clear? At least, that's what I was thinking when starting to work on the issue. I'm certain we could update this code (in a separate PR) to make it more readable. I'm also not sure if we need all featured that the Iban data structure provides 🤷

Yes, all good
It was just and only for clarification
There is no questionmark or similar on my statement 😄

@matthewmayer
Copy link
Contributor

This PR changes the used algorithm from type c to a which then does not generate digits anymore

I guess you answered that because the implementation wasn't that clear? At least, that's what I was thinking when starting to work on the issue. I'm certain we could update this code (in a separate PR) to make it more readable. I'm also not sure if we need all featured that the Iban data structure provides 🤷

I agree the IBAN code is quite incomprehensible. Even some minor changes like defining a constant for "c" and "a" at top of the file would help readability

@xDivisionByZerox
Copy link
Member Author

I agree with both of you. I'll work on a second PR to make this more readable. I'm just wondering whether we should consider performance metrics here. While I could immediately think of multiple ways of how the implementation could be written more readable, all would definitely be slower (mostly due to multiple iterations over the expected pattern).
I'm always torn when it comes to performance in the faker library. What do you think? Or should I open a separate issue to discuss this?

@xDivisionByZerox xDivisionByZerox merged commit 7b12056 into next Apr 5, 2025
34 checks passed
@xDivisionByZerox xDivisionByZerox deleted the fix/more-strict-iban-for-IE-and-PS branch April 5, 2025 17:52
@Shinigami92
Copy link
Member

[...] I'm always torn when it comes to performance in the faker library. What do you think? Or should I open a separate issue to discuss this?

I'm also most times in favor of readability and maintainability over performance. JS is not the language of choice when it comes to processing millions of data efficiently anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working m: finance Something is referring to the finance module p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancies with iban generation for PS and IE
3 participants