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

Enhance cardholder name validation #134

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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 33 additions & 4 deletions src/__tests__/cardholder-name.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cardholderName } from "../cardholder-name";
import type { Verification } from "../types";
import { cvv } from '../cvv'

describe("cardholderName", () => {
describe.each([
Expand All @@ -23,10 +24,10 @@ describe("cardholderName", () => {
],

[
"returns false strings that are longer than 255 characters",
"returns false strings that are longer than 24 characters",
[
[
"this name is 256 chracters aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"this name is 25 chracters",
{ isValid: false, isPotentiallyValid: false },
],
],
Expand All @@ -38,12 +39,12 @@ describe("cardholderName", () => {
["name", { isValid: true, isPotentiallyValid: true }],
["given sur", { isValid: true, isPotentiallyValid: true }],
[
"this name is 255 chracters aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"this name is 24 charssss",
{ isValid: true, isPotentiallyValid: true },
],
["name with many spaces", { isValid: true, isPotentiallyValid: true }],
[
"name with number in it 01234",
"with number in it 01234",
{ isValid: true, isPotentiallyValid: true },
],
],
Expand All @@ -70,4 +71,32 @@ describe("cardholderName", () => {
});
},
);

describe("maxLength", () => {
it("defaults maxLength to 24", () => {
expect(cardholderName("this name is 25 chracters")).toEqual({
isValid: false,
isPotentiallyValid: false,
});
expect(cardholderName("this name is 24 charssss")).toEqual({ isValid: true, isPotentiallyValid: true });
});

it("accepts maxLength", () => {
expect(cardholderName("this name is 25 chracters", 25)).toEqual({
isValid: true,
isPotentiallyValid: true,
});
});

it("returns invalid if beyond maxLength", () => {
expect(cardholderName("this name is 25 chracters")).toEqual({
isValid: false,
isPotentiallyValid: false,
});
expect(cardholderName("this name is 26 characters", 25)).toEqual({
isValid: false,
isPotentiallyValid: false,
});
});
});
});
11 changes: 8 additions & 3 deletions src/cardholder-name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Verification } from "./types";

const CARD_NUMBER_REGEX = /^[\d\s-]*$/;
const MAX_LENGTH = 255;
const DEFAULT_LENGTH = 24;

function verification(
isValid: boolean,
Expand All @@ -10,7 +10,10 @@ function verification(
return { isValid, isPotentiallyValid };
}

export function cardholderName(value: string | unknown): Verification {
export function cardholderName(
value: string | unknown,
maxLength: number = DEFAULT_LENGTH,
): Verification {
if (typeof value !== "string") {
return verification(false, false);
}
Expand All @@ -19,10 +22,12 @@ export function cardholderName(value: string | unknown): Verification {
return verification(false, true);
}

if (value.length > MAX_LENGTH) {
if (value.length > maxLength) {
return verification(false, false);
}

// If the cardholder name only contains numbers, hyphens, and spaces, it's not valid,
// but it may still be potentially valid if a non-numeric character is added.
if (CARD_NUMBER_REGEX.test(value)) {
return verification(false, true);
}
Expand Down