-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cardholder name validation (#86)
* Add cardholder name validation addresses #33 * Update src/__tests__/cardholder-name.ts Co-authored-by: Holly Stotelmyer <[email protected]> * Update src/__tests__/cardholder-name.ts * prettier Co-authored-by: Holly Stotelmyer <[email protected]>
- Loading branch information
1 parent
4c9266f
commit ad2456d
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# unreleased | ||
|
||
- Add `cardholderName` verification method | ||
|
||
# 8.0.0 | ||
|
||
_Breaking Changes_ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { cardholderName } from "../cardholder-name"; | ||
import type { Verification } from "../types"; | ||
|
||
describe("cardholderName", () => { | ||
describe.each([ | ||
[ | ||
"returns false for non-string types", | ||
[ | ||
[0, { isValid: false, isPotentiallyValid: false }], | ||
[0, { isValid: false, isPotentiallyValid: false }], | ||
[123, { isValid: false, isPotentiallyValid: false }], | ||
[1234, { isValid: false, isPotentiallyValid: false }], | ||
[12345, { isValid: false, isPotentiallyValid: false }], | ||
[557016, { isValid: false, isPotentiallyValid: false }], | ||
[-1234, { isValid: false, isPotentiallyValid: false }], | ||
[-10, { isValid: false, isPotentiallyValid: false }], | ||
[0 / 0, { isValid: false, isPotentiallyValid: false }], | ||
[Infinity, { isValid: false, isPotentiallyValid: false }], | ||
[null, { isValid: false, isPotentiallyValid: false }], | ||
[[], { isValid: false, isPotentiallyValid: false }], | ||
[{}, { isValid: false, isPotentiallyValid: false }], | ||
], | ||
], | ||
|
||
[ | ||
"returns false strings that are longer than 255 characters", | ||
[ | ||
[ | ||
"this name is 256 chracters aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
{ isValid: false, isPotentiallyValid: false }, | ||
], | ||
], | ||
], | ||
|
||
[ | ||
"accepts valid cardholder names", | ||
[ | ||
["name", { isValid: true, isPotentiallyValid: true }], | ||
["given sur", { isValid: true, isPotentiallyValid: true }], | ||
[ | ||
"this name is 255 chracters aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
{ isValid: true, isPotentiallyValid: true }, | ||
], | ||
["name with many spaces", { isValid: true, isPotentiallyValid: true }], | ||
[ | ||
"name with number in it 01234", | ||
{ isValid: true, isPotentiallyValid: true }, | ||
], | ||
], | ||
], | ||
|
||
[ | ||
"returns isPotentiallyValid for shorter-than-1 strings", | ||
[["", { isValid: false, isPotentiallyValid: true }]], | ||
], | ||
|
||
[ | ||
"returns isPotentiallyValid for strings with only numbers, spaces, and hyphens", | ||
[ | ||
["4111", { isValid: false, isPotentiallyValid: true }], | ||
["4111 1111", { isValid: false, isPotentiallyValid: true }], | ||
["4111-1111", { isValid: false, isPotentiallyValid: true }], | ||
], | ||
], | ||
] as Array<[string, Array<[string, Verification]>]>)( | ||
"%s", | ||
(description, tests) => { | ||
it.each(tests)("parses %s to be %p", (parseMe, meta) => { | ||
expect(cardholderName(parseMe)).toEqual(meta); | ||
}); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Verification } from "./types"; | ||
|
||
const CARD_NUMBER_REGEX = /^[\d\s-]*$/; | ||
const MAX_LENGTH = 255; | ||
|
||
function verification( | ||
isValid: boolean, | ||
isPotentiallyValid: boolean | ||
): Verification { | ||
return { isValid, isPotentiallyValid }; | ||
} | ||
|
||
export function cardholderName(value: string | unknown): Verification { | ||
if (typeof value !== "string") { | ||
return verification(false, false); | ||
} | ||
|
||
if (value.length === 0) { | ||
return verification(false, true); | ||
} | ||
|
||
if (value.length > MAX_LENGTH) { | ||
return verification(false, false); | ||
} | ||
|
||
if (CARD_NUMBER_REGEX.test(value)) { | ||
return verification(false, true); | ||
} | ||
|
||
return verification(true, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters