Skip to content

Commit f303d39

Browse files
authoredAug 3, 2023
feat(isIBAN): add white and blacklist options to the isIBAN validator (#2235)
* add white and blacklist options to the isIBAN validator * improve coverage for isIBAN validator * docs: add an explanation for the options object in the isIBAN validator without formatting it * fix: remove errors from the isIBAN validator
1 parent 2ef9a83 commit f303d39

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Validator | Description
121121
**isHexadecimal(str)** | check if the string is a hexadecimal number.
122122
**isHexColor(str)** | check if the string is a hexadecimal color.
123123
**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
124-
**isIBAN(str)** | check if the string is an IBAN (International Bank Account Number).
124+
**isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).<br/><br/>`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`.
125125
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.<br/><br/>Defaults to 'any'.
126126
**isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. IMEI should be of format `###############` or `##-######-######-#`.<br/><br/>`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format. If `allow_hyphens` is set to true, the validator will validate the second format.
127127
**isIn(str, values)** | check if the string is in an array of allowed values.

‎src/lib/isIBAN.js

+46-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ const ibanRegexThroughCountryCode = {
8787
XK: /^(XK[0-9]{2})\d{16}$/,
8888
};
8989

90+
/**
91+
* Check if the country codes passed are valid using the
92+
* ibanRegexThroughCountryCode as a reference
93+
*
94+
* @param {array} countryCodeArray
95+
* @return {boolean}
96+
*/
97+
98+
function hasOnlyValidCountryCodes(countryCodeArray) {
99+
const countryCodeArrayFilteredWithObjectIbanCode = countryCodeArray
100+
.filter(countryCode => !(countryCode in ibanRegexThroughCountryCode));
101+
102+
if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) {
103+
return false;
104+
}
105+
106+
return true;
107+
}
108+
90109
/**
91110
* Check whether string has correct universal IBAN format
92111
* The IBAN consists of up to 34 alphanumeric characters, as follows:
@@ -96,14 +115,37 @@ const ibanRegexThroughCountryCode = {
96115
* NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z]
97116
*
98117
* @param {string} str - string under validation
118+
* @param {object} options - object to pass the countries to be either whitelisted or blacklisted
99119
* @return {boolean}
100120
*/
101-
function hasValidIbanFormat(str) {
121+
function hasValidIbanFormat(str, options) {
102122
// Strip white spaces and hyphens
103123
const strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase();
104124
const isoCountryCode = strippedStr.slice(0, 2).toUpperCase();
105125

106-
return (isoCountryCode in ibanRegexThroughCountryCode) &&
126+
const isoCountryCodeInIbanRegexCodeObject = isoCountryCode in ibanRegexThroughCountryCode;
127+
128+
if (options.whitelist) {
129+
if (!hasOnlyValidCountryCodes(options.whitelist)) {
130+
return false;
131+
}
132+
133+
const isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode);
134+
135+
if (!isoCountryCodeInWhiteList) {
136+
return false;
137+
}
138+
}
139+
140+
if (options.blacklist) {
141+
const isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode);
142+
143+
if (isoCountryCodeInBlackList) {
144+
return false;
145+
}
146+
}
147+
148+
return (isoCountryCodeInIbanRegexCodeObject) &&
107149
ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr);
108150
}
109151

@@ -131,10 +173,10 @@ function hasValidIbanChecksum(str) {
131173
return remainder === 1;
132174
}
133175

134-
export default function isIBAN(str) {
176+
export default function isIBAN(str, options = {}) {
135177
assertString(str);
136178

137-
return hasValidIbanFormat(str) && hasValidIbanChecksum(str);
179+
return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str);
138180
}
139181

140182
export const locales = Object.keys(ibanRegexThroughCountryCode);

‎test/validators.test.js

+77
Original file line numberDiff line numberDiff line change
@@ -5256,6 +5256,83 @@ describe('Validators', () => {
52565256
'FR763000600001123456!!🤨7890189@',
52575257
],
52585258
});
5259+
test({
5260+
validator: 'isIBAN',
5261+
args: [{ whitelist: ['DK', 'GB'] }],
5262+
valid: [
5263+
'DK5000400440116243',
5264+
'GB29NWBK60161331926819',
5265+
],
5266+
invalid: [
5267+
'BE71 0961 2345 6769',
5268+
'FR76 3000 6000 0112 3456 7890 189',
5269+
'DE91 1000 0000 0123 4567 89',
5270+
'GR96 0810 0010 0000 0123 4567 890',
5271+
'RO09 BCYP 0000 0012 3456 7890',
5272+
'SA44 2000 0001 2345 6789 1234',
5273+
'ES79 2100 0813 6101 2345 6789',
5274+
'XX22YYY1234567890123',
5275+
'FR14 2004 1010 0505 0001 3',
5276+
'FR7630006000011234567890189@',
5277+
'FR7630006000011234567890189😅',
5278+
'FR763000600001123456!!🤨7890189@',
5279+
],
5280+
});
5281+
test({
5282+
validator: 'isIBAN',
5283+
args: [{ whitelist: ['XX', 'AA'] }],
5284+
invalid: [
5285+
'DK5000400440116243',
5286+
'GB29NWBK60161331926819',
5287+
'BE71 0961 2345 6769',
5288+
'FR76 3000 6000 0112 3456 7890 189',
5289+
'DE91 1000 0000 0123 4567 89',
5290+
'GR96 0810 0010 0000 0123 4567 890',
5291+
'RO09 BCYP 0000 0012 3456 7890',
5292+
'SA44 2000 0001 2345 6789 1234',
5293+
'ES79 2100 0813 6101 2345 6789',
5294+
'XX22YYY1234567890123',
5295+
'FR14 2004 1010 0505 0001 3',
5296+
'FR7630006000011234567890189@',
5297+
'FR7630006000011234567890189😅',
5298+
'FR763000600001123456!!🤨7890189@',
5299+
],
5300+
});
5301+
test({
5302+
validator: 'isIBAN',
5303+
args: [{ blacklist: ['IT'] }],
5304+
valid: [
5305+
'SC52BAHL01031234567890123456USD',
5306+
'LC14BOSL123456789012345678901234',
5307+
'MT31MALT01100000000000000000123',
5308+
'SV43ACAT00000000000000123123',
5309+
'EG800002000156789012345180002',
5310+
'BE71 0961 2345 6769',
5311+
'FR76 3000 6000 0112 3456 7890 189',
5312+
'DE91 1000 0000 0123 4567 89',
5313+
'GR96 0810 0010 0000 0123 4567 890',
5314+
'RO09 BCYP 0000 0012 3456 7890',
5315+
'SA44 2000 0001 2345 6789 1234',
5316+
'ES79 2100 0813 6101 2345 6789',
5317+
'CH56 0483 5012 3456 7800 9',
5318+
'GB98 MIDL 0700 9312 3456 78',
5319+
'IL170108000000012612345',
5320+
'JO71CBJO0000000000001234567890',
5321+
'TR320010009999901234567890',
5322+
'BR1500000000000010932840814P2',
5323+
'LB92000700000000123123456123',
5324+
'IR200170000000339545727003',
5325+
'MZ97123412341234123412341',
5326+
],
5327+
invalid: [
5328+
'XX22YYY1234567890123',
5329+
'FR14 2004 1010 0505 0001 3',
5330+
'FR7630006000011234567890189@',
5331+
'FR7630006000011234567890189😅',
5332+
'FR763000600001123456!!🤨7890189@',
5333+
'IT60X0542811101000000123456',
5334+
],
5335+
});
52595336
});
52605337

52615338
it('should validate BIC codes', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.