card-validator 9.0.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -76,6 +76,8 @@ You can optionally pass `maxLength` as a property of an object as a second argum
76
76
 
77
77
  If a card brand has a normal max length that is shorter than the passed in max length, the validator will use the shorter one. For instance, if a `maxLength` of `16` is provided, the validator will still use `15` as the max length for American Express cards.
78
78
 
79
+ You can optionally pass `skipLuhnValidation: true` as a property of an object as a second argument. This will override the default behaviour and will skip the validation of the check digit of the card number using Luhn Algorithm. The `skipLuhnValidation` **should not** be set to `true` in production environment.
80
+
79
81
  ```javascript
80
82
  valid.number(<Maestro Card with 19 Digits>, {maxLength: 16});
81
83
 
@@ -16,6 +16,7 @@ export interface CardNumberVerification extends Verification {
16
16
  type CardNumberOptions = {
17
17
  maxLength?: number;
18
18
  luhnValidateUnionPay?: boolean;
19
+ skipLuhnValidation?: boolean;
19
20
  };
20
21
  export declare function cardNumber(value: string | unknown, options?: CardNumberOptions): CardNumberVerification;
21
22
  export {};
@@ -31,8 +31,9 @@ function cardNumber(value, options) {
31
31
  if (options.maxLength && testCardValue.length > options.maxLength) {
32
32
  return verification(cardType, false, false);
33
33
  }
34
- if (cardType.type === getCardTypes.types.UNIONPAY &&
35
- options.luhnValidateUnionPay !== true) {
34
+ if (options.skipLuhnValidation === true ||
35
+ (cardType.type === getCardTypes.types.UNIONPAY &&
36
+ options.luhnValidateUnionPay !== true)) {
36
37
  isValid = true;
37
38
  }
38
39
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "card-validator",
3
- "version": "9.0.0",
3
+ "version": "9.1.0",
4
4
  "description": "A library for validating credit card fields",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -349,4 +349,25 @@ describe("number validates", () => {
349
349
  expect(actual.isValid).toBe(true);
350
350
  });
351
351
  });
352
+
353
+ describe("Skip Luhn Validation", () => {
354
+ const number = "5732806135556590";
355
+ it("should fail validation for a card with invalid luhn check digit", () => {
356
+ const actual = cardNumber(number);
357
+
358
+ expect(actual.card.type).toBe("maestro");
359
+ expect(actual.isPotentiallyValid).toBe(true);
360
+ expect(actual.isValid).toBe(false);
361
+ });
362
+
363
+ it("should succeed validation for a card with invalid luhn check digit when using skipLuhnValidation=true", () => {
364
+ const actual = cardNumber(number, {
365
+ skipLuhnValidation: true,
366
+ });
367
+
368
+ expect(actual.card.type).toBe("maestro");
369
+ expect(actual.isPotentiallyValid).toBe(true);
370
+ expect(actual.isValid).toBe(true);
371
+ });
372
+ });
352
373
  });
@@ -22,6 +22,7 @@ export interface CardNumberVerification extends Verification {
22
22
  type CardNumberOptions = {
23
23
  maxLength?: number;
24
24
  luhnValidateUnionPay?: boolean;
25
+ skipLuhnValidation?: boolean;
25
26
  };
26
27
 
27
28
  function verification(
@@ -67,8 +68,9 @@ export function cardNumber(
67
68
  }
68
69
 
69
70
  if (
70
- cardType.type === getCardTypes.types.UNIONPAY &&
71
- options.luhnValidateUnionPay !== true
71
+ options.skipLuhnValidation === true ||
72
+ (cardType.type === getCardTypes.types.UNIONPAY &&
73
+ options.luhnValidateUnionPay !== true)
72
74
  ) {
73
75
  isValid = true;
74
76
  } else {