Skip to content

Commit 06c93d1

Browse files
authored
Merge pull request #5 from adia-technology/feature/validation-result
Added validation result in place of boolean
2 parents 1998c48 + 4f8dfaf commit 06c93d1

File tree

8 files changed

+43
-25
lines changed

8 files changed

+43
-25
lines changed

CHANGELOG.md

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

3+
## 0.3.0
4+
5+
* **BREAKING** Changed `validateSsn`'s return type from boolean to `ValidationResult`. This allows to return the reason for which the SSN is invalid.
6+
37
## 0.2.0
48

59
* Made `CountryCode` type public.

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ npm install @adia-technology/international-ssn-validator --save
1313
### Usage
1414

1515
```js
16-
import { validateSsn } from '@adia-technology/international-ssn-validator';
16+
import { validateSsn, ValidationResult } from '@adia-technology/international-ssn-validator';
1717

18-
let isValid = validateSsn('756.1111.2222.00', 'ch');
18+
let validationResult = validateSsn('756.1111.2222.00', 'ch');
19+
let isValid = validationResult === ValidationResult.Valid;
1920
```
2021

2122
## API
@@ -25,6 +26,8 @@ This library exposes a single function, `validateSsn`, that accepts two paramete
2526
* `ssn : string` - SSN to validate.
2627
* `countryCode : string` - country for which to check the SSN against. Currently supported are `us` and `ch`.
2728

29+
It returns a `ValidationResult`, which essentially is a string enum being set to one of the values: `'VALID'`, `'INVALID_FORMAT'` or `'INVALID_CHECKSUM'`.
30+
2831
## Dependencies
2932

3033
This library uses the following other libraries:

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.2.0",
3+
"version": "0.3.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/ValidationResult.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum ValidationResult {
2+
Valid = "VALID",
3+
InvalidFormat = "INVALID_FORMAT",
4+
InvalidChecksum = "INVALID_CHECKSUM"
5+
}

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { validateSwissSsn } from './validateSwissSsn';
22
import { validateUsSsn } from './validateUsSsn';
3+
import { ValidationResult } from './ValidationResult';
34

45
export type CountryCode = 'us' | 'ch';
56

6-
export function validateSsn(ssn : string, countryCode : CountryCode) : boolean {
7+
export { ValidationResult };
8+
9+
export function validateSsn(ssn : string, countryCode : CountryCode) : ValidationResult {
710

811
if (countryCode == null || countryCode as string === '') {
912
throw new Error('countryCode was not provided');

src/validateSwissSsn.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import AhvValidator from 'ahv13-validator';
2+
import { ValidationResult } from './ValidationResult';
23

34
const ahvRegex = /^756\.\d{4}\.\d{4}\.\d{2}$/;
45

5-
export function validateSwissSsn(ssn ?: string) : boolean {
6+
export function validateSwissSsn(ssn ?: string) : ValidationResult {
67
if (ssn == null || !ahvRegex.test(ssn)) {
7-
return false;
8+
return ValidationResult.InvalidFormat;
89
}
910

1011
let ahvValidator = new AhvValidator();
11-
return ahvValidator.isValid(ssn);
12+
return ahvValidator.isValid(ssn) ? ValidationResult.Valid : ValidationResult.InvalidChecksum;
1213
}

src/validateUsSsn.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isValid } from 'ssn-validator';
2+
import { ValidationResult } from './ValidationResult';
23

3-
export function validateUsSsn(ssn ?: string) : boolean {
4-
return isValid(ssn);
4+
export function validateUsSsn(ssn ?: string) : ValidationResult {
5+
return isValid(ssn) ? ValidationResult.Valid : ValidationResult.InvalidFormat;
56
}

test/index.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { validateSsn, CountryCode } from '../src';
2+
import { ValidationResult } from '../src/ValidationResult';
23

34
test('Given empty SSN, result should not be valid', () => {
45
let invalidSsn = '';
56

6-
expect(validateSsn(invalidSsn, 'ch')).toBe(false);
7-
expect(validateSsn(invalidSsn, 'us')).toBe(false);
7+
expect(validateSsn(invalidSsn, 'ch')).toBe(ValidationResult.InvalidFormat);
8+
expect(validateSsn(invalidSsn, 'us')).toBe(ValidationResult.InvalidFormat);
89
});
910

1011
test('Given null SSN, result should not be valid', () => {
1112
let invalidSsn = null;
1213

13-
expect(validateSsn(invalidSsn, 'ch')).toBe(false);
14-
expect(validateSsn(invalidSsn, 'us')).toBe(false);
14+
expect(validateSsn(invalidSsn, 'ch')).toBe(ValidationResult.InvalidFormat);
15+
expect(validateSsn(invalidSsn, 'us')).toBe(ValidationResult.InvalidFormat);
1516
});
1617

1718
test('Given undefined SSN, result should not be valid', () => {
1819
let invalidSsn = undefined;
1920

20-
expect(validateSsn(invalidSsn, 'ch')).toBe(false);
21-
expect(validateSsn(invalidSsn, 'us')).toBe(false);
21+
expect(validateSsn(invalidSsn, 'ch')).toBe(ValidationResult.InvalidFormat);
22+
expect(validateSsn(invalidSsn, 'us')).toBe(ValidationResult.InvalidFormat);
2223
});
2324

2425
test('Given valid Swiss SSN, when validateSsn called with "ch" country code, result should be valid', () => {
@@ -35,13 +36,13 @@ test('Given valid Swiss SSN, when validateSsn called with "ch" country code, res
3536
];
3637

3738
for (let validSsn of validSwissSsns) {
38-
expect(validateSsn(validSsn, 'ch')).toBe(true);
39+
expect(validateSsn(validSsn, 'ch')).toBe(ValidationResult.Valid);
3940
}
4041
});
4142

4243
test('Given valid Swiss SSN, when validateSsn called with "us" country code, result should be invalid', () => {
4344
let validSwissSsn = "756.5220.1643.81";
44-
expect(validateSsn(validSwissSsn, 'us')).toBe(false);
45+
expect(validateSsn(validSwissSsn, 'us')).toBe(ValidationResult.InvalidFormat);
4546
});
4647

4748
test('Given invalid Swiss SSN, result should be invalid', () => {
@@ -52,12 +53,12 @@ test('Given invalid Swiss SSN, result should be invalid', () => {
5253
let noChecksum = "756.0246.1057.4";
5354
let tooLong = "756.8478.9370.660";
5455

55-
expect(validateSsn(wrongChecksum, 'ch')).toBe(false);
56-
expect(validateSsn(wrongPrefix, 'ch')).toBe(false);
57-
expect(validateSsn(noDots, 'ch')).toBe(false);
58-
expect(validateSsn(wrongSeparators, 'ch')).toBe(false);
59-
expect(validateSsn(noChecksum, 'ch')).toBe(false);
60-
expect(validateSsn(tooLong, 'ch')).toBe(false);
56+
expect(validateSsn(wrongChecksum, 'ch')).toBe(ValidationResult.InvalidChecksum);
57+
expect(validateSsn(wrongPrefix, 'ch')).toBe(ValidationResult.InvalidFormat);
58+
expect(validateSsn(noDots, 'ch')).toBe(ValidationResult.InvalidFormat);
59+
expect(validateSsn(wrongSeparators, 'ch')).toBe(ValidationResult.InvalidFormat);
60+
expect(validateSsn(noChecksum, 'ch')).toBe(ValidationResult.InvalidFormat);
61+
expect(validateSsn(tooLong, 'ch')).toBe(ValidationResult.InvalidFormat);
6162
});
6263

6364
test('Given valid US SSN, when validateSsn called with "us" country code, result should be valid', () => {
@@ -67,13 +68,13 @@ test('Given valid US SSN, when validateSsn called with "us" country code, result
6768
];
6869

6970
for (let validSsn of validUsSsns) {
70-
expect(validateSsn(validSsn, 'us')).toBe(true);
71+
expect(validateSsn(validSsn, 'us')).toBe(ValidationResult.Valid);
7172
}
7273
});
7374

7475
test('Given valid US SSN, when validateSsn called with "ch" country code, result should be invalid', () => {
7576
let validUsSsn = "011-23-4567";
76-
expect(validateSsn(validUsSsn, 'ch')).toBe(false);
77+
expect(validateSsn(validUsSsn, 'ch')).toBe(ValidationResult.InvalidFormat);
7778
});
7879

7980
test('Given unsupported countryCode, validateSsn should throw an error', () => {

0 commit comments

Comments
 (0)