Skip to content

Commit

Permalink
DTBTWEB-125 Make whitespace postal code invalid (#122)
Browse files Browse the repository at this point in the history
Update postal-code validation

- to trim leading/trailing white-space
- test for 3-char min postal code length
- verify that 1st 3 characters are alphanumeric.

Co-authored-by: corydavis <corydavis@paypal.com>
  • Loading branch information
ibooker and CJGlitter authored Jun 20, 2024
1 parent 2d1ea1f commit 3b6f942
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# 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
- Update `braces` to 3.0.3

# 9.1.0

- Support skipping of luhn check digit validation in card number validator.

# 9.0.0
Expand Down Expand Up @@ -50,7 +55,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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/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
{
Expand All @@ -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});
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/postal-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down Expand Up @@ -87,6 +97,10 @@ describe("postalCode", () => {
isValid: true,
isPotentiallyValid: true,
});
expect(postalCode(" -$%", { minLength: 0 })).toEqual({
isValid: false,
isPotentiallyValid: true,
});
});
});
});
3 changes: 3 additions & 0 deletions src/postal-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 3b6f942

Please sign in to comment.