Repository: validatorjs/validator.js
Pull request #2008 (2022-07-27 05:13:33)

Feat: enchance isCreditCard
Add regex validation for AmEx card

rubiin: hey there, instead of making a case specific validator, I would suggest something like isDebitcard or isCreditCard followed by provider as a param 
brianwhaley: something like this?

`import assertString from './util/assertString';
import isLuhnValid from './isLuhnValid';

/* eslint-disable max-len */
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
const amexCard = /^3[47][0-9]{13}$/;
/* eslint-enable max-len */

export default function isCreditCard(card, provider) {
  assertString(card);
  const sanitized = card.replace(/[- ]+/g, '');
  switch(provider.toLowerCase()) {
    case "amex":
      if (!amexCard.test(sanitized)) {
        return false;
      }
      break;
    default:
      if (!creditCard.test(sanitized)) {
        return false;
      }
  }
  return isLuhnValid(card);
}`
brianwhaley: hey @rubiin i have refactored the isCreditCard file, will be submitting a new PR shortly.  Thanks for you suggestions!
codecov[bot]: # [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) Report
> Merging [#2008](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) (9bdd5eb) into [master](https://codecov.io/gh/validatorjs/validator.js/commit/1bb14e85dd7fb8cec8a43ff586c0d869977cd684?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) (1bb14e8) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##            master     #2008   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          104       104           
  Lines         2203      2209    +6     
  Branches       477       480    +3     
=========================================
+ Hits          2203      2209    +6     
```


| [Impacted Files](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) | Coverage Δ | |
|---|---|---|
| [src/lib/isCreditCard.js](https://codecov.io/gh/validatorjs/validator.js/pull/2008/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs#diff-c3JjL2xpYi9pc0NyZWRpdENhcmQuanM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs). Last update [1bb14e8...9bdd5eb](https://codecov.io/gh/validatorjs/validator.js/pull/2008?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).

brianwhaley: Thank you @rubiin.   @profnandaa when you have an opportunity please do look at the PR.  Much appreciated!  
brianwhaley: apologies, thought this was approved and started cleaning up too soon.  PR is reopened and waiting for approval from @profnandaa 
brianwhaley: @rubiin  - i did modify the existing isCreditCard file, and ensured it was backwards compatible.  can you evaluate the diff?  you will see you can still pass in a credit card number without the second param, and still get the same result.  Thanks
brianwhaley: Hi @rubiin and @profnandaa - is there an additional person that can approve this PR?  I would like to get it merged to main.  Thank you!
rubiin: Lets wait for @profnandaa 
brianwhaley: @profnandaa  and @rubiin , is there an additional person that can approve this PR? I would like to get it merged to main. Thank you!
brianwhaley: @WikiRik would you be able to review this PR ?  Thank you!
WikiRik: @brianwhaley I can, but my review won't get this merged faster unfortunately. We'll have to wait for @profnandaa 

Either way you can still use this specific commit in other projects, see this [NPM docs](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#git-urls-as-dependencies)
brianwhaley: @profnandaa and @rubiin , this PR has been open for almost a month.  Is there someone we can add to be the final reviewer?  Thank you! 
braaar: I would suggest you wrap the `providers` optional parameter in an object. See this issue for more context about that https://github.com/validatorjs/validator.js/issues/1874

As you can see from the discussion there this has not been cemented as the official guideline yet, but I think we should push in that direction.
brianwhaley: @braaar thanks for the feedback, made the adjustment for that.  

cc: @profnandaa and @rubiin
brianwhaley: @braaar @profnandaa @rubiin @chriso @ezkemboi @tux-tn 
This PR is open for almost 2 months.  It would be helpful for me and my team if we could review and close this PR.   What can I do to get this over the finish line?  Thank you! 

Patch:
diff --git a/README.md b/README.md
index 961b50d..5e31fa8 100644
--- a/README.md
+++ b/README.md
@@ -103,14 +103,14 @@ Validator                               | Description
 **isBoolean(str [, options])**          | check if a string is a boolean.<br/>`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']).
 **isBtcAddress(str)**            | check if the string is a valid BTC address.
 **isByteLength(str [, options])**          | check if the string's length (in UTF-8 bytes) falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`.
-**isCreditCard(str)**                   | check if the string is a credit card.
+**isCreditCard(card, [, options])**                   | check if the string is a credit card.<br/><br/>  options is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card.  Valid values include `amex` , `dinersclub` , `discover` , `jcb` , `mastercard` , `unionpay` , `visa` or blank will check for any provider. 
 **isCurrency(str [, options])**            | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.<br/>**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
 **isDataURI(str)**                      | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
 **isDate(input [, options])**          | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].<br/><br/> `options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`<br/><br/>`format` is a string and defaults to `YYYY/MM/DD`.<br/><br/>`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.<br/><br/> `delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`.
 **isDecimal(str [, options])**             | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`<br/><br/>`locale` determine the decimal separator and 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', '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', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
 **isDivisibleBy(str, number)**          | check if the string is a number that's divisible by another.
 **isEAN(str)**                          | check if the string is an EAN (European Article Number).
-**isEmail(str [, options])**            | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails.
+**isEmail(str [, options])**            | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. 
 **isEmpty(str [, options])**            | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace:false }`.
 **isEthereumAddress(str)**              | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums.
 **isFloat(str [, options])**            | check if the string is a float.<br/><br/>`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`locale` determine the decimal separator and 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', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`.
@@ -154,7 +154,7 @@ Validator                               | Description
 **isMultibyte(str)**                    | check if the string contains one or more multibyte chars.
 **isNumeric(str [, options])**                      | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}` it also has locale as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).<br/><br/>`locale` determine the decimal separator and 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', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
 **isOctal(str)**                        | check if the string is a valid octal number.
-**isPassportNumber(str, countryCode)**    | check if the string is a valid passport number.<br/><br/>(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'ID', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`.
+**isPassportNumber(str, countryCode)**    | check if the string is a valid passport number.<br/><br/>(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'ID', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US' ]`.
 **isPort(str)**                         | check if the string is a valid port number.
 **isPostalCode(str, locale)**           | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
 **isRFC3339(str)**                      | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.
diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js
index 53c80eb..0a874c9 100644
--- a/src/lib/isCreditCard.js
+++ b/src/lib/isCreditCard.js
@@ -1,15 +1,34 @@
 import assertString from './util/assertString';
 import isLuhnValid from './isLuhnValid';
 
+const cards = {
+  amex: /^3[47][0-9]{13}$/,
+  dinersclub: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
+  discover: /^6(?:011|5[0-9][0-9])[0-9]{12,15}$/,
+  jcb: /^(?:2131|1800|35\d{3})\d{11}$/,
+  mastercard: /^5[1-5][0-9]{2}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/, // /^[25][1-7][0-9]{14}$/;
+  unionpay: /^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
+  visa: /^(?:4[0-9]{12})(?:[0-9]{3,6})?$/,
+};
 /* eslint-disable max-len */
-const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
+const allCards = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
 /* eslint-enable max-len */
 
-export default function isCreditCard(str) {
-  assertString(str);
-  const sanitized = str.replace(/[- ]+/g, '');
-  if (!creditCard.test(sanitized)) {
+export default function isCreditCard(card, options = {}) {
+  assertString(card);
+  const { provider } = options;
+  const sanitized = card.replace(/[- ]+/g, '');
+  if (provider && provider.toLowerCase() in cards) {
+    // specific provider in the list
+    if (!(cards[provider.toLowerCase()].test(sanitized))) {
+      return false;
+    }
+  } else if (provider && !(provider.toLowerCase() in cards)) {
+    /* specific provider not in the list */
+    throw new Error(`${provider} is not a valid credit card provider.`);
+  } else if (!(allCards.test(sanitized))) {
+    // no specific provider
     return false;
   }
-  return isLuhnValid(str);
+  return isLuhnValid(card);
 }
diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js
index c7b7d6e..6db0019 100644
--- a/src/lib/isEmail.js
+++ b/src/lib/isEmail.js
@@ -13,6 +13,7 @@ const default_email_options = {
   blacklisted_chars: '',
   ignore_max_length: false,
   host_blacklist: [],
+  host_whitelist: [],
 };
 
 /* eslint-disable max-len */
@@ -99,6 +100,10 @@ export default function isEmail(str, options) {
     return false;
   }
 
+  if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) {
+    return false;
+  }
+
   let user = parts.join('@');
 
   if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {
diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js
index c434a70..5b2ebfd 100644
--- a/src/lib/isPassportNumber.js
+++ b/src/lib/isPassportNumber.js
@@ -55,6 +55,7 @@ const passportRegexByCountryCode = {
   SE: /^\d{8}$/, // SWEDEN
   SL: /^(P)[A-Z]\d{7}$/, // SLOVANIA
   SK: /^[0-9A-Z]\d{7}$/, // SLOVAKIA
+  TH: /^[A-Z]{1,2}\d{6,7}$/, // THAILAND
   TR: /^[A-Z]\d{8}$/, // TURKEY
   UA: /^[A-Z]{2}\d{6}$/, // UKRAINE
   US: /^\d{9}$/, // UNITED STATES


Test patch:
diff --git a/test/validators.js b/test/validators.js
index 87f73cc..e74d5fc 100644
--- a/test/validators.js
+++ b/test/validators.js
@@ -344,6 +344,22 @@ describe('Validators', () => {
     });
   });
 
+  it('should validate only email addresses with whitelisted domains', () => {
+    test({
+      validator: 'isEmail',
+      args: [{ host_whitelist: ['gmail.com', 'foo.bar.com'] }],
+      valid: [
+        'email@gmail.com',
+        'test@foo.bar.com',
+      ],
+      invalid: [
+        'foo+bar@test.com',
+        'email@foo.com',
+        'email@bar.com',
+      ],
+    });
+  });
+
   it('should validate URLs', () => {
     test({
       validator: 'isURL',
@@ -3374,6 +3390,22 @@ describe('Validators', () => {
       ],
     });
 
+    test({
+      validator: 'isPassportNumber',
+      args: ['TH'],
+      valid: [
+        'A123456',
+        'B1234567',
+        'CD123456',
+        'EF1234567',
+      ],
+      invalid: [
+        '123456',
+        '1234567',
+        '010485371AA',
+      ],
+    });
+
     test({
       validator: 'isPassportNumber',
       args: ['TR'],
@@ -5128,6 +5160,276 @@ describe('Validators', () => {
     });
   });
 
+
+  it('should validate credit cards without a proper provider', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'Plorf' }],
+      error: [
+        'foo',
+        // valid cc #
+        '375556917985515',
+        '4716-2210-5188-5662',
+        '375556917985515999999993',
+        '6234917882863855suffix',
+      ],
+    });
+  });
+
+
+  it('should validate AmEx provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'AmEx' }],
+      valid: [
+        '375556917985515',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '36050234196908',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '5398228707871527',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+
+  it('should validate Diners Club provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'DinersClub' }],
+      valid: [
+        '36050234196908',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '375556917985515',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '5398228707871527',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+  it('should validate Discover provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'Discover' }],
+      valid: [
+        '6011111111111117',
+        '6011000990139424',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '36050234196908',
+        '375556917985515',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '5398228707871527',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+  it('should validate JCB provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'JCB' }],
+      valid: [
+        '3530111333300000',
+        '3566002020360505',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '36050234196908',
+        '375556917985515',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '5398228707871527',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+
+  it('should validate Mastercard provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'Mastercard' }],
+      valid: [
+        '2222155765072228',
+        '2225855203075256',
+        '2718760626256570',
+        '2720428011723762',
+        '5398228707871527',
+      ],
+      invalid: [
+        'foo',
+        '36050234196908',
+        '375556917985515',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+
+  it('should validate Union Pay provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'UnionPay' }],
+      valid: [
+        '6226050967750613',
+        '6234917882863855',
+        '6234698580215388',
+        '6246281879460688',
+        '6263892624162870',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '36050234196908',
+        '375556917985515',
+        '375556917985515999999993',
+        '4716461583322103',
+        '4716-2210-5188-5662',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+        '5398228707871527',
+        '6234917882863855suffix',
+      ],
+    });
+  });
+
+
+  it('should validate Visa provided credit cards', () => {
+    test({
+      validator: 'isCreditCard',
+      args: [{ provider: 'Visa' }],
+      valid: [
+        '4716-2210-5188-5662',
+        '4716461583322103',
+        '4716989580001715211',
+        '4929 7226 5379 7141',
+      ],
+      invalid: [
+        'foo',
+        '2222155765072228',
+        '2225855203075256',
+        '2720428011723762',
+        '2718760626256570',
+        '36050234196908',
+        '375556917985515',
+        '375556917985515999999993',
+        '5398228707871527',
+        '6234917882863855suffix',
+        '6283875070985593',
+        '6263892624162870',
+        '6234917882863855',
+        '6234698580215388',
+        '6226050967750613',
+        '6246281879460688',
+        '6283875070985593',
+        '6765780016990268',
+        '8171999927660000',
+        '8171999900000000021',
+      ],
+    });
+  });
+
+
   it('should validate identity cards', () => {
     const fixtures = [
       {