From c9310369603e8717c105f13e20afdaf261911035 Mon Sep 17 00:00:00 2001 From: "Brian T. Whaley" Date: Mon, 22 Aug 2022 15:49:08 -0400 Subject: [PATCH 1/2] changed providers to optional parameter in options object --- README.md | 2 +- src/lib/isCreditCard.js | 28 ++++- test/validators.js | 270 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8977228c8..6954a8ed6 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Validator | Description **isBoolean(str [, options])** | check if a string is a boolean.
`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.

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

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.

`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}`.
**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()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 53c80eb80..2e44ba3ed 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -1,15 +1,33 @@ 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}))$/; /* 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 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/test/validators.js b/test/validators.js index 87f73ccd4..293a8a3a9 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5128,6 +5128,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 = [ { From 0eaded683150141a7aa504663718d6182b224284 Mon Sep 17 00:00:00 2001 From: "Brian T. Whaley" Date: Mon, 22 Aug 2022 15:54:49 -0400 Subject: [PATCH 2/2] changed providers to optional parameter in options object --- src/lib/isCreditCard.js | 3 ++- test/validators.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 2e44ba3ed..0a874c9cf 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -11,11 +11,12 @@ const cards = { 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(card, options = {}) { assertString(card); + const { provider } = options; const sanitized = card.replace(/[- ]+/g, ''); if (provider && provider.toLowerCase() in cards) { // specific provider in the list diff --git a/test/validators.js b/test/validators.js index 293a8a3a9..761e82e3a 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5397,7 +5397,7 @@ describe('Validators', () => { }); }); - + it('should validate identity cards', () => { const fixtures = [ {