Skip to content

Commit

Permalink
feat: Hipercard validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskemper committed Dec 29, 2023
1 parent b12249e commit 4d6ce9a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
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

0 comments on commit 4d6ce9a

Please sign in to comment.