card-validator 9.0.0 → 10.0.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
 
@@ -343,7 +345,7 @@ The `cvv` validation by default tests for a numeric string of 3 characters in le
343
345
 
344
346
  #### `valid.postalCode(value: string, [options: object]): object`
345
347
 
346
- The `postalCode` validation essentially tests for a valid string greater than 3 characters in length.
348
+ The `postalCode` validation essentially ignores leading/trailing whitespace and tests for a valid string greater than 3 characters in length. It also verifies that the first 3 letters are alphanumeric.
347
349
 
348
350
  ```javascript
349
351
  {
@@ -352,7 +354,7 @@ The `postalCode` validation essentially tests for a valid string greater than 3
352
354
  }
353
355
  ```
354
356
 
355
- You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3.
357
+ You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3 and verify that the characters for the specified length are all alphanumeric.
356
358
 
357
359
  ```javascript
358
360
  valid.postalCode('123', {minLength: 5});
@@ -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 {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.postalCode = void 0;
4
4
  var DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;
5
+ var ALPHANUM = new RegExp(/^[a-z0-9]+$/i);
5
6
  function verification(isValid, isPotentiallyValid) {
6
7
  return { isValid: isValid, isPotentiallyValid: isPotentiallyValid };
7
8
  }
@@ -14,6 +15,9 @@ function postalCode(value, options) {
14
15
  else if (value.length < minLength) {
15
16
  return verification(false, true);
16
17
  }
18
+ else if (!ALPHANUM.test(value.trim().slice(0, minLength))) {
19
+ return verification(false, true);
20
+ }
17
21
  return verification(true, true);
18
22
  }
19
23
  exports.postalCode = postalCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "card-validator",
3
- "version": "9.0.0",
3
+ "version": "10.0.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
  });
@@ -41,6 +41,16 @@ describe("postalCode", () => {
41
41
  [["hello world", { isValid: true, isPotentiallyValid: true }]],
42
42
  ],
43
43
 
44
+ [
45
+ "returns isPotentiallyValid for strings without alphanumeric first three characters",
46
+ [
47
+ ["123", { isValid: true, isPotentiallyValid: true }],
48
+ [" 123", { isValid: true, isPotentiallyValid: true }],
49
+ ["---", { isValid: false, isPotentiallyValid: true }],
50
+ [" ---", { isValid: false, isPotentiallyValid: true }],
51
+ ],
52
+ ],
53
+
44
54
  [
45
55
  "returns isPotentiallyValid for shorter-than-3 strings",
46
56
  [
@@ -87,6 +97,10 @@ describe("postalCode", () => {
87
97
  isValid: true,
88
98
  isPotentiallyValid: true,
89
99
  });
100
+ expect(postalCode(" -$%", { minLength: 0 })).toEqual({
101
+ isValid: false,
102
+ isPotentiallyValid: true,
103
+ });
90
104
  });
91
105
  });
92
106
  });
@@ -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 {
@@ -5,6 +5,7 @@ type PostalCodeOptions = {
5
5
  };
6
6
 
7
7
  const DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;
8
+ const ALPHANUM = new RegExp(/^[a-z0-9]+$/i);
8
9
 
9
10
  function verification(
10
11
  isValid: boolean,
@@ -23,6 +24,8 @@ export function postalCode(
23
24
  return verification(false, false);
24
25
  } else if (value.length < minLength) {
25
26
  return verification(false, true);
27
+ } else if (!ALPHANUM.test(value.trim().slice(0, minLength))) {
28
+ return verification(false, true);
26
29
  }
27
30
 
28
31
  return verification(true, true);