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

DTBTWEB-125 Make whitespace postal code invalid #122

Merged
5 commits merged into from
Jun 20, 2024
Merged
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
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 }],
jplukarski marked this conversation as resolved.
Show resolved Hide resolved
[" ---", { 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
Loading