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

feat: Hipercard validation #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions library/src/validations/creditCard/creditCard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ describe('creditCard', () => {
expect(validate._parse(value10).output).toBe(value10);
const value11 = '30218047196557';
expect(validate._parse(value11).output).toBe(value11);
const value12 = '6062825624254001';
expect(validate._parse(value12).output).toBe(value12);
const value13 = '6062823936268330';
expect(validate._parse(value13).output).toBe(value13);
const value14 = '38410012946684';
expect(validate._parse(value14).output).toBe(value14);
const value15 = '3841 6058 9902 54';
expect(validate._parse(value15).output).toBe(value15);
});

test('should reject invalid cards', () => {
const validate = creditCard();

expect(validate._parse('').issues).toBeTruthy();
expect(validate._parse('1234 5678 9012 3456').issues).toBeTruthy();
Expand All @@ -37,6 +49,8 @@ describe('creditCard', () => {
expect(validate._parse('4111 1111 1111 111').issues).toBeTruthy();
expect(validate._parse('abcd efgh ijkl mnop').issues).toBeTruthy();
expect(validate._parse('0000 0000 0000 0000').issues).toBeTruthy();
expect(validate._parse('60628260434272857').issues).toBeTruthy();
expect(validate._parse('5121647321685222').issues).toBeTruthy();
});

test('should return custom error message', () => {
Expand Down
32 changes: 18 additions & 14 deletions library/src/validations/creditCard/creditCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,28 @@ export type CreditCardValidation<TInput extends string> =
*/
const SANITIZE_REGEX = /[- ]+/gu;

export const AMERICAN_EXPRESS_REGEX = /^3[47]\d{13}$/u;
export const DINERS_CLUB_REGEX = /^3(?:0[0-5]|[68]\d)\d{11}$/u;
export const DISCOVER_REGEX = /^6(?:011|5\d{2})\d{12,15}$/u;
export const JCB_REGEX = /^(?:2131|1800|35\d{3})\d{11}$/u;
export const MASTERCARD_REGEX =
/^5[1-5]\d{2}|(222\d|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)\d{12}$/u;
export const UNION_PAY_REGEX = /^(6[27]\d{14}|81\d{14,17})$/u;
export const VISA_REGEX = /^4\d{12}(?:\d{3,6})?$/u;
export const HIPERCARD_REGEX = /^(606282\d{10}(\d{3})?)|(3841\d{13})$/u;

/**
* Provider regex list.
*/
const PROVIDER_REGEX_LIST = [
// American Express
/^3[47]\d{13}$/u,
// Diners Club
/^3(?:0[0-5]|[68]\d)\d{11}$/u,
// Discover
/^6(?:011|5\d{2})\d{12,15}$/u,
// JCB
/^(?:2131|1800|35\d{3})\d{11}$/u,
// Mastercard
/^5[1-5]\d{2}|(222\d|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)\d{12}$/u,
// UnionPay
/^(6[27]\d{14}|81\d{14,17})$/u,
// Visa
/^4\d{12}(?:\d{3,6})?$/u,
AMERICAN_EXPRESS_REGEX,
DINERS_CLUB_REGEX,
DISCOVER_REGEX,
JCB_REGEX,
MASTERCARD_REGEX,
UNION_PAY_REGEX,
VISA_REGEX,
HIPERCARD_REGEX,
];

/**
Expand Down