Skip to content

Commit 1fe49b7

Browse files
committed
feat: Add Aadhaar validation
Indian Profile Number
1 parent dff0a91 commit 1fe49b7

File tree

6 files changed

+74
-31
lines changed

6 files changed

+74
-31
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isValid_Aadhaar_Number } from '../aadhaar-validation';
2+
3+
describe('isValid_Aadhaar_Number', () => {
4+
it('should return false for null input', () => {
5+
expect(isValid_Aadhaar_Number(null as any)).toBe(false);
6+
});
7+
8+
it('should return false for Aadhaar numbers with incorrect format', () => {
9+
expect(isValid_Aadhaar_Number('1234 5678 9012')).toBe(false);
10+
expect(isValid_Aadhaar_Number('23456789012')).toBe(false);
11+
expect(isValid_Aadhaar_Number('2345-6789-0123')).toBe(false);
12+
});
13+
14+
it('should return true for valid Aadhaar numbers', () => {
15+
expect(isValid_Aadhaar_Number('2345 6789 0123')).toBe(true);
16+
expect(isValid_Aadhaar_Number('3456 7890 1234')).toBe(true);
17+
});
18+
19+
it('should return false for Aadhaar numbers starting with 1', () => {
20+
expect(isValid_Aadhaar_Number('1234 5678 9012')).toBe(false);
21+
});
22+
23+
it('should return false for Aadhaar numbers with spaces in wrong places', () => {
24+
expect(isValid_Aadhaar_Number('234 56789 0123')).toBe(false);
25+
});
26+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { isValid_SSN } from '../ssn-validation';
2+
3+
describe('isValid_SSN', () => {
4+
it('should return false for SSN with length not equal to 9', () => {
5+
expect(isValid_SSN('123')).toBe(false);
6+
expect(isValid_SSN('1234567890')).toBe(false);
7+
});
8+
9+
it('should return false for SSN starting with 9', () => {
10+
expect(isValid_SSN('912345678')).toBe(false);
11+
});
12+
13+
it('should return false for SSN with invalid area numbers', () => {
14+
expect(isValid_SSN('000123456')).toBe(false);
15+
expect(isValid_SSN('666123456')).toBe(false);
16+
});
17+
18+
it('should return false for SSN with invalid group numbers', () => {
19+
expect(isValid_SSN('123001234')).toBe(false);
20+
});
21+
22+
it('should return false for SSN with invalid serial numbers', () => {
23+
expect(isValid_SSN('123450000')).toBe(false);
24+
});
25+
26+
it('should return true for valid SSN', () => {
27+
expect(isValid_SSN('123456789')).toBe(true);
28+
});
29+
});

src/__tests__/ssn-validatons.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/aadhaar-validation.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** Funtion to validate Aadhaar Number */
2+
export const isValid_Aadhaar_Number = (aadhaar_number: string): boolean => {
3+
4+
// Regex to check valid
5+
// aadhaar_number
6+
let regex = new RegExp(/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/);
7+
8+
// if aadhaar_number
9+
// is empty return false
10+
if (aadhaar_number == null) {
11+
return false;
12+
}
13+
14+
// Return true if the aadhaar_number
15+
// matched the ReGex
16+
return regex.test(aadhaar_number);
17+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './aadhaar-validation';
12
export * from './ssn-validation';

src/ssn-validation.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11

22
/**
33
* Function to validate SSN based on defined rules
4-
param value
54
*/
6-
export const validateSSN = (value: string): boolean => {
5+
export const isValid_SSN = (value: string): boolean => {
76
const invalidSSPatterns = ['000','666'];
87
if ((value.length >= 1 && value. length !== 9) || value[0] === '9' || invalidSSPatterns.includes(value.slice(0, 3)) || value.slice(3, 5) === '00' || value.slice(5, 9) === '0000') {
98
return false;

0 commit comments

Comments
 (0)