Skip to content

Commit 1998c48

Browse files
authored
Merge pull request #4 from adia-technology/error-handling
Exported CountryCode, added additional error handling
2 parents 5724dfe + 7c23844 commit 1998c48

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.2.0
4+
5+
* Made `CountryCode` type public.
6+
* Improved error handling.
7+
38
## 0.1.1
49

510
Removed dependencies from the bundle.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adia-technology/international-ssn-validator",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "A utility to validate social security number of different countries.",
55
"main": "dist/index.js",
66
"module": "dist/index.es2015.js",

src/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { validateSwissSsn } from './validateSwissSsn';
22
import { validateUsSsn } from './validateUsSsn';
33

4-
type CountryCode = 'us' | 'ch';
4+
export type CountryCode = 'us' | 'ch';
55

66
export function validateSsn(ssn : string, countryCode : CountryCode) : boolean {
7-
switch (countryCode) {
8-
case 'us':
9-
return validateUsSsn(ssn);
10-
case 'ch':
11-
return validateSwissSsn(ssn);
12-
default:
13-
throw new Error('');
14-
}
7+
8+
if (countryCode == null || countryCode as string === '') {
9+
throw new Error('countryCode was not provided');
10+
}
11+
12+
switch (countryCode) {
13+
case 'us':
14+
return validateUsSsn(ssn);
15+
case 'ch':
16+
return validateSwissSsn(ssn);
17+
default:
18+
throw new Error(`'${countryCode}' country code is not supported.`);
19+
}
1520
}

src/validateSwissSsn.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import AhvValidator from 'ahv13-validator';
33
const ahvRegex = /^756\.\d{4}\.\d{4}\.\d{2}$/;
44

55
export function validateSwissSsn(ssn ?: string) : boolean {
6-
if (ssn == null || !ahvRegex.test(ssn)) {
7-
return false;
8-
}
6+
if (ssn == null || !ahvRegex.test(ssn)) {
7+
return false;
8+
}
99

10-
let ahvValidator = new AhvValidator();
11-
return ahvValidator.isValid(ssn);
10+
let ahvValidator = new AhvValidator();
11+
return ahvValidator.isValid(ssn);
1212
}

test/index.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateSsn } from '../src';
1+
import { validateSsn, CountryCode } from '../src';
22

33
test('Given empty SSN, result should not be valid', () => {
44
let invalidSsn = '';
@@ -75,3 +75,12 @@ test('Given valid US SSN, when validateSsn called with "ch" country code, result
7575
let validUsSsn = "011-23-4567";
7676
expect(validateSsn(validUsSsn, 'ch')).toBe(false);
7777
});
78+
79+
test('Given unsupported countryCode, validateSsn should throw an error', () => {
80+
expect(() => validateSsn('000', 'pl' as CountryCode)).toThrowError("'pl' country code is not supported.");
81+
});
82+
83+
test('Given empty or null countryCode, validateSsn should throw an error', () => {
84+
expect(() => validateSsn('000', '' as CountryCode)).toThrowError('countryCode was not provided');
85+
expect(() => validateSsn('000', null)).toThrowError('countryCode was not provided');
86+
});

0 commit comments

Comments
 (0)