Skip to content

Commit

Permalink
Support an optional prop to skip luhn algo check in card number valid…
Browse files Browse the repository at this point in the history
…ator (#119)

* Support an optional param to skip luhn algo check

* update readme
  • Loading branch information
ravishekhar authored Feb 8, 2024
1 parent 8fdb47c commit bd68741
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
npm-debug.log

dist
.idea
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# UNRELEASED
- Support skipping of luhn check digit validation in card number validator.

# 9.0.0

- BREAKING CHANGES
- Update node to v18
-DevDependency Updates:
-DevDependency Updates:
- Update prettier to v3
- Update eslint-plugin-prettier to v5

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ You can optionally pass `maxLength` as a property of an object as a second argum

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.

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.

```javascript
valid.number(<Maestro Card with 19 Digits>, {maxLength: 16});

Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/card-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,25 @@ describe("number validates", () => {
expect(actual.isValid).toBe(true);
});
});

describe("Skip Luhn Validation", () => {
const number = "5732806135556590";
it("should fail validation for a card with invalid luhn check digit", () => {
const actual = cardNumber(number);

expect(actual.card.type).toBe("maestro");
expect(actual.isPotentiallyValid).toBe(true);
expect(actual.isValid).toBe(false);
});

it("should succeed validation for a card with invalid luhn check digit when using skipLuhnValidation=true", () => {
const actual = cardNumber(number, {
skipLuhnValidation: true,
});

expect(actual.card.type).toBe("maestro");
expect(actual.isPotentiallyValid).toBe(true);
expect(actual.isValid).toBe(true);
});
});
});
6 changes: 4 additions & 2 deletions src/card-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface CardNumberVerification extends Verification {
type CardNumberOptions = {
maxLength?: number;
luhnValidateUnionPay?: boolean;
skipLuhnValidation?: boolean;
};

function verification(
Expand Down Expand Up @@ -67,8 +68,9 @@ export function cardNumber(
}

if (
cardType.type === getCardTypes.types.UNIONPAY &&
options.luhnValidateUnionPay !== true
options.skipLuhnValidation === true ||
(cardType.type === getCardTypes.types.UNIONPAY &&
options.luhnValidateUnionPay !== true)
) {
isValid = true;
} else {
Expand Down

0 comments on commit bd68741

Please sign in to comment.