Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support an optional prop to skip luhn algo check in card number validator #119

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2,7 +2,7 @@
import getCardTypes = require("credit-card-type");
import type { Verification } from "./types";

// TODO this should probably come from credit-card-type

Check warning on line 5 in src/card-number.ts

View workflow job for this annotation

GitHub Actions / Unit Tests on Ubuntu

Unexpected 'todo' comment: 'TODO this should probably come from...'
type CreditCardType = {
niceType: string;
type: string;
Expand All @@ -22,6 +22,7 @@
type CardNumberOptions = {
maxLength?: number;
luhnValidateUnionPay?: boolean;
skipLuhnValidation?: boolean;
};

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

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
Loading