-
-
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
feat(isIBAN): add white and blacklist options to the isIBAN validator #2235
Changes from 2 commits
b76d131
53ecf0b
e81e6d7
bd34c0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,25 @@ const ibanRegexThroughCountryCode = { | |
XK: /^(XK[0-9]{2})\d{16}$/, | ||
}; | ||
|
||
/** | ||
* Check if the country codes passed are valid using the | ||
* ibanRegexThroughCountryCode as a reference | ||
* | ||
* @param {array} countryCodeArray | ||
* @return {boolean} | ||
*/ | ||
|
||
function hasOnlyValidCountryCodes(countryCodeArray) { | ||
const countryCodeArrayFilteredWithObjectIbanCode = countryCodeArray | ||
.filter(countryCode => !(countryCode in ibanRegexThroughCountryCode)); | ||
|
||
if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Check whether string has correct universal IBAN format | ||
* The IBAN consists of up to 34 alphanumeric characters, as follows: | ||
|
@@ -95,14 +114,37 @@ const ibanRegexThroughCountryCode = { | |
* NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z] | ||
* | ||
* @param {string} str - string under validation | ||
* @param {object} options - object to pass the countries to be either whitelisted or blacklisted | ||
* @return {boolean} | ||
*/ | ||
function hasValidIbanFormat(str) { | ||
function hasValidIbanFormat(str, options) { | ||
// Strip white spaces and hyphens | ||
const strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase(); | ||
const isoCountryCode = strippedStr.slice(0, 2).toUpperCase(); | ||
|
||
return (isoCountryCode in ibanRegexThroughCountryCode) && | ||
const isoCountryCodeInIbanRegexCodeObject = isoCountryCode in ibanRegexThroughCountryCode; | ||
|
||
if (options.whitelist) { | ||
if (!hasOnlyValidCountryCodes(options.whitelist)) { | ||
throw new Error('One of the codes passed is invalid'); | ||
} | ||
|
||
const isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode); | ||
|
||
if (!isoCountryCodeInWhiteList) { | ||
throw new Error('IBAN code does not belong to one of the countries listed on whitelist!'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the pattern so far has not been to throw errors, just true/false. I'm not sure how to go about this one... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we can continue with the true/false approach and let the user decide how to treat this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done @profnandaa |
||
} | ||
} | ||
|
||
if (options.blacklist) { | ||
const isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode); | ||
|
||
if (isoCountryCodeInBlackList) { | ||
throw new Error('IBAN code belongs to one of the countries listed on blacklist!'); | ||
} | ||
} | ||
|
||
return (isoCountryCodeInIbanRegexCodeObject) && | ||
ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr); | ||
} | ||
|
||
|
@@ -130,10 +172,10 @@ function hasValidIbanChecksum(str) { | |
return remainder === 1; | ||
} | ||
|
||
export default function isIBAN(str) { | ||
export default function isIBAN(str, options = {}) { | ||
assertString(str); | ||
|
||
return hasValidIbanFormat(str) && hasValidIbanChecksum(str); | ||
return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str); | ||
} | ||
|
||
export const locales = Object.keys(ibanRegexThroughCountryCode); |
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.
Could you elaborate on how you can define this? Like we explain the
options
object in other validators as well. Also with the list of supported country codesThere 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.
@WikiRik I have updated and put which attributes it will receive and also the values that can be used in each of them.