From 69f7739d351eeb4a88aff9f2f1ef10ae6c610d8e Mon Sep 17 00:00:00 2001 From: Iris Booker Date: Tue, 28 May 2024 16:32:52 -0500 Subject: [PATCH 1/4] fix: Make whitespace postal code invalid --- src/__tests__/postal-code.ts | 14 ++++++++++++++ src/postal-code.ts | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/__tests__/postal-code.ts b/src/__tests__/postal-code.ts index 025e6bf..8f8aeaf 100644 --- a/src/__tests__/postal-code.ts +++ b/src/__tests__/postal-code.ts @@ -41,6 +41,16 @@ describe("postalCode", () => { [["hello world", { isValid: true, isPotentiallyValid: true }]], ], + [ + "returns isPotentiallyValid for strings without alphanumeric first three characters", + [ + ["123", { isValid: true, isPotentiallyValid: true }], + [" 123", { isValid: true, isPotentiallyValid: true }], + ["---", { isValid: false, isPotentiallyValid: true }], + [" ---", { isValid: false, isPotentiallyValid: true }], + ], + ], + [ "returns isPotentiallyValid for shorter-than-3 strings", [ @@ -87,6 +97,10 @@ describe("postalCode", () => { isValid: true, isPotentiallyValid: true, }); + expect(postalCode(" -$%", { minLength: 0 })).toEqual({ + isValid: false, + isPotentiallyValid: true, + }); }); }); }); diff --git a/src/postal-code.ts b/src/postal-code.ts index 45aa425..2fe2897 100644 --- a/src/postal-code.ts +++ b/src/postal-code.ts @@ -5,6 +5,7 @@ type PostalCodeOptions = { }; const DEFAULT_MIN_POSTAL_CODE_LENGTH = 3; +const ALPHANUM = new RegExp(/^[a-z0-9]+$/i); function verification( isValid: boolean, @@ -23,6 +24,8 @@ export function postalCode( return verification(false, false); } else if (value.length < minLength) { return verification(false, true); + } else if (!ALPHANUM.test(value.trim().slice(0, minLength))) { + return verification(false, true); } return verification(true, true); From 733d151700b20c4e03109e95073780c8f2f522d8 Mon Sep 17 00:00:00 2001 From: corydavis Date: Wed, 29 May 2024 17:02:51 +0000 Subject: [PATCH 2/4] docs: Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e043fb..4824a59 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,7 @@ The `cvv` validation by default tests for a numeric string of 3 characters in le #### `valid.postalCode(value: string, [options: object]): object` -The `postalCode` validation essentially tests for a valid string greater than 3 characters in length. +The `postalCode` validation essentially ignores leading whitespace and tests for a valid string greater than 3 characters in length. It also verifies that the first 3 letters are alphanumeric. ```javascript { @@ -354,7 +354,7 @@ The `postalCode` validation essentially tests for a valid string greater than 3 } ``` -You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3. +You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3 and verify that the characters for the specified length are all alphanumeric. ```javascript valid.postalCode('123', {minLength: 5}); From 9ccb5d3318188e185fe7458aeb7bdbd452b7a8c0 Mon Sep 17 00:00:00 2001 From: Iris Booker Date: Thu, 6 Jun 2024 14:30:47 -0500 Subject: [PATCH 3/4] docs: update changelog --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b969364..4bb5366 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ +# UNRELEASED + +- Update postal code validation to: + - strip trailing and leading whitespace + - verify postal code is at least 3 characters + - confirm 1st three characters are alphanumeric + # 9.1.0 + - Support skipping of luhn check digit validation in card number validator. # 9.0.0 @@ -46,7 +54,7 @@ _Breaking Changes_ # 6.1.0 -- Add option to set a `maxLength` for card number valiation +- Add option to set a `maxLength` for card number validation # 6.0.0 From f2b5a9a20621503a6d8ec092cc8f6765ad6fd2aa Mon Sep 17 00:00:00 2001 From: corydavis Date: Thu, 6 Jun 2024 19:35:06 +0000 Subject: [PATCH 4/4] Update README for clarity --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4824a59..e8aaeab 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,7 @@ The `cvv` validation by default tests for a numeric string of 3 characters in le #### `valid.postalCode(value: string, [options: object]): object` -The `postalCode` validation essentially ignores leading whitespace and tests for a valid string greater than 3 characters in length. It also verifies that the first 3 letters are alphanumeric. +The `postalCode` validation essentially ignores leading/trailing whitespace and tests for a valid string greater than 3 characters in length. It also verifies that the first 3 letters are alphanumeric. ```javascript {