Skip to content
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

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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.

Copy link
Contributor Author

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:

Screenshot_Amex.jpg

There is no need to add the locale param.

Copy link
Member

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...

Copy link
Contributor Author

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. 😁

**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import isHSL from './lib/isHSL';

import isISRC from './lib/isISRC';

import isAbaRouting from './lib/isAbaRouting';
import isIBAN, { locales as ibanLocales } from './lib/isIBAN';
import isBIC from './lib/isBIC';

Expand Down Expand Up @@ -141,6 +142,7 @@ const validator = {
isIPRange,
isFQDN,
isBoolean,
isAbaRouting,
isIBAN,
isBIC,
isAlpha,
Expand Down
20 changes: 20 additions & 0 deletions src/lib/isAbaRouting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assertString from './util/assertString';

// http://www.brainjar.com/js/validation/
const isRoutingReg = /^[0-9]{9}$/;
Copy link
Contributor

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)

[1] https://s3-eu-west-1.amazonaws.com/cjp-rbi-accuity/wp-content/uploads/2016/09/21222732/ROUTING_NUMBER_POLICY-2016.pdf

Copy link
Contributor Author

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!


export default function isAbaRouting(str) {
assertString(str);
str = str.trim();
Copy link
Contributor

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.


if (!isRoutingReg.exec(str)) return false;
songyuew marked this conversation as resolved.
Show resolved Hide resolved

const strArr = str.split('');
Copy link
Contributor

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.

let checkSumVal = 0;
for (let i = 0; i < strArr.length; i++) {
if (i % 3 === 0) checkSumVal += strArr[i] * 3;
else if (i % 3 === 1) checkSumVal += strArr[i] * 7;
else checkSumVal += strArr[i] * 1;
}
return (checkSumVal % 10 === 0);
}
19 changes: 19 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5103,6 +5103,25 @@ describe('Validators', () => {
});
});

it('should validate ABA routing number', () => {
test({
validator: 'isAbaRouting',
valid: [
'322070381',
'789456124',
'011103093',
'263170175',
'124303065',
],
invalid: [
'426317017',
'qwerty',
'12430306',
'382070381',
],
});
});

it('should validate IBAN', () => {
test({
validator: 'isIBAN',
Expand Down