-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Added isAbaRouting validator #2143
Conversation
Codecov ReportBase: 100.00% // Head: 100.00% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #2143 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 106 107 +1
Lines 2348 2361 +13
Branches 593 597 +4
=========================================
+ Hits 2348 2361 +13
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
src/lib/isAbaRouting.js
Outdated
|
||
if (!isRoutingReg.exec(str)) return false; | ||
|
||
const strArr = str.split(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not mistaken I don't think it is necessary to split the string's characters into an array ->
you can access each character in the string via its index directly as well.
I've also tested it locally here and it works with the tests you provided.
src/lib/isAbaRouting.js
Outdated
import assertString from './util/assertString'; | ||
|
||
// http://www.brainjar.com/js/validation/ | ||
const isRoutingReg = /^[0-9]{9}$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the ABA document [1] (page 4), there seems to be quite a few ranges, that are "reserved for future use", i.e. they are currently not valid ABA numbers, at least that is the way I would see it.
I think it would make sense to adjust the RegExp accordingly to reflect that, what do you think?
If you agree, don't forget to also update the tests, as I can see at least one potentially "invalid" test case that is currently in the "valid" section (789456124
would fall under these "reserved for future use" number ranges)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Panogiotis,
You are right about this. I have just double-checked their latest article. I added a separate regex to filter out reserved routing numbers, so that it can be easily modified if reserved series is brought into assigned in the future.
Thanks for your review!
Hi Songyue, thanks for the updates. We can make make this happen with one regexp only, by using a "negative lookahead" like so:
I do agree, that this is a tiny bit harder to read though, but this seems to be the fastest way to get this done :-) If you agree, would you update the RegExp accordingly please? Out of curiosity (and also a tad bit of boredom ;-)) I also did try out two other ways to achieve the same thing, while keeping it a bit easier to read. Attempt 1: Use an Array for the reservedRanged and join them into a new RegExp
Attempt 2: Use String Concatenation,
Benchmark tests:
Fastest is messy single line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, looks good to me now :-)
(however please note: I am not a maintainer, so my approval is not really as helpful for getting this merged - we will need to wait for the maintainers to approve and pull - and unfortunuately this might take some time)
one tiny small side note, just to avoid any further questions:
the Regexp theoretically can still be tightened, e.g. that last part (9[0-2])|(9[3-9])
could be combined into a single (9[0-9])
, however I would say it makes sense to keep this "uncombined" as it is now, as this mimics the "official" ranges from the ABA document
Hi Panagiotis, Yes, I have the same opinion, as they are defined as two Thank you again for your proposals! They are all sound and practical. Just for my curiosity, for the RegEx benchmarking, which tool did you use? It is very useful to get number of ops per sec. |
I am using Benchmark.js for these, as it is pretty straight forward to set up those comparisons. However from what I read online, we of course should always be "cautious" with such benchmark tools, as they may lead to "inaccurate" results or wrong assumptions sometimes, depending on the test case. |
src/lib/isAbaRouting.js
Outdated
|
||
export default function isAbaRouting(str) { | ||
assertString(str); | ||
str = str.trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about not noticing before, but I would say that str.trim()
probably does not belong here?
In my opinion trimming the string falls under the "sanitation step" and should be kept out of this "validation step".
Otherwise you might end up with a scenario as below:
- User inputs
<space>teststring<space>
- validator trims the string from
- validator runs its validation on the now internally sanitized input and returns e.g.
true
- user thinks that their string is valid, when in reality the validator actually validated the sanitized string
- user ends up continuing working with the untrimmed (theoretically) invalid string
Does that make sense?
Let me know, what you think.
Looks good to me now, that the sanitization is removed :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please fix the m/c and we'll be good to go.
Thank you @profnandaa , M/C now fixed. |
@@ -90,6 +90,7 @@ Validator | Description | |||
--------------------------------------- | -------------------------------------- | |||
**contains(str, seed [, options])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false.<br/>`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. | |||
**equals(str, comparison)** | check if the string matches the comparison. | |||
**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, this is just for US only? Do we plan to support other countries and should we have a locale param then?
Sorry missed this in my previous review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, ABA is the abbv for American Bankers Association. The ABA number is used with every US bank account and cheque, to identify the corresponding financial institution. For example:
There is no need to add the locale param.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does any other country have a similar thing perhaps just with a different name? Trying to see if there's a way we could generalize here than having a whole validator just for one country...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea. I'll look into it. 😁
Added isAbaRouting validator
Check whether a string is ABA routing number for US bank account & cheque
https://en.wikipedia.org/wiki/ABA_routing_transit_number
Please review my PR, thanks!
Checklist