Skip to content

Commit 9224fb0

Browse files
committed
Added IP Validation
1 parent 7731b99 commit 9224fb0

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install validate-functions
1515
- **Email Validation**: Ensures that email addresses conform to standard formats.
1616
- **SSN Validation**: Validates U.S. Social Security Numbers based on predefined rules.
1717
- **Aadhaar Validation**: Checks the validity of Indian Aadhaar numbers using specific patterns.
18-
- **Credit Card**: The credit card validation uses the Luhn algorithm to verify the validity of credit card numbers structurally.
19-
18+
- **Credit Card Validation**: The credit card validation uses the Luhn algorithm to verify the validity of credit card numbers structurally.
19+
- **IP Validation**: IPv4 and IPv6 validation for specififcations.
2020

2121
Usage Details available at [here](./USAGE_DETAILS.md).

USAGE_DETAILS.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,21 @@ import { isValid_Credit_Card } from 'validate-functions/credit-card';
3535
const creditCardNumber = '4539 1488 0343 6467'; // Example of a valid Visa card number
3636
console.log(isValid_Credit_Card(creditCardNumber)); // Output: true if valid, false otherwise
3737
```
38+
39+
- **isValid_IPv4**
40+
41+
```typescript
42+
import { isValid_IPv4 } from 'validate-functions/ip';
43+
44+
const ipv4Address = '192.168.1.1';
45+
console.log(isValid_IPv4(ipv4Address)); // Output: true if valid, false otherwise
46+
```
47+
48+
- **isValid_IPv6**
49+
50+
```typescript
51+
import { isValid_IPv6 } from 'validate-functions/ip';
52+
53+
const ipv6Address = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
54+
console.log(isValid_IPv6(ipv6Address)); // Output: true if valid, false otherwise
55+
```

src/__tests__/ip.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { isValid_IPv4, isValid_IPv6 } from '../ip';
2+
3+
describe('IPv4 Validation', () => {
4+
it('should return true for a valid IPv4 address', () => {
5+
const validIPv4 = '172.16.254.1';
6+
expect(isValid_IPv4(validIPv4)).toBe(true);
7+
});
8+
9+
it('should return false for an invalid IPv4 address', () => {
10+
const invalidIPv4 = '256.256.256.256';
11+
expect(isValid_IPv4(invalidIPv4)).toBe(false);
12+
});
13+
14+
it('should return false for an incomplete IPv4 address', () => {
15+
const incompleteIPv4 = '192.168.1';
16+
expect(isValid_IPv4(incompleteIPv4)).toBe(false);
17+
});
18+
19+
it('should return false for a non-numeric IPv4 address', () => {
20+
const nonNumericIPv4 = 'abc.def.ghi.jkl';
21+
expect(isValid_IPv4(nonNumericIPv4)).toBe(false);
22+
});
23+
24+
it('should return true for edge case IPv4 address 0.0.0.0', () => {
25+
const edgeCaseIPv4 = '0.0.0.0';
26+
expect(isValid_IPv4(edgeCaseIPv4)).toBe(true);
27+
});
28+
29+
it('should return true for edge case IPv4 address 255.255.255.255', () => {
30+
const edgeCaseIPv4 = '255.255.255.255';
31+
expect(isValid_IPv4(edgeCaseIPv4)).toBe(true);
32+
});
33+
});
34+
35+
describe('IPv6 Validation', () => {
36+
it('should return true for a valid IPv6 address', () => {
37+
const validIPv6 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
38+
expect(isValid_IPv6(validIPv6)).toBe(true);
39+
});
40+
41+
it('should return false for an invalid IPv6 address', () => {
42+
const invalidIPv6 = '2001:db8::1:2::3';
43+
expect(isValid_IPv6(invalidIPv6)).toBe(false);
44+
});
45+
46+
it('should return false for an incomplete IPv6 address', () => {
47+
const incompleteIPv6 = '2001:0db8:85a3:0000:0000:8a2e:0370';
48+
expect(isValid_IPv6(incompleteIPv6)).toBe(false);
49+
});
50+
51+
it('should return false for a non-hexadecimal IPv6 address', () => {
52+
const nonHexIPv6 = '2001:0db8:85a3:zzzz:0000:8a2e:0370:7334';
53+
expect(isValid_IPv6(nonHexIPv6)).toBe(false);
54+
});
55+
56+
it('should return true for edge case IPv6 address ::1 (loopback)', () => {
57+
const loopbackIPv6 = '::1';
58+
expect(isValid_IPv6(loopbackIPv6)).toBe(true);
59+
});
60+
61+
it('should return true for edge case IPv6 address :: (unspecified)', () => {
62+
const unspecifiedIPv6 = '::';
63+
expect(isValid_IPv6(unspecifiedIPv6)).toBe(true);
64+
});
65+
});

src/credit-card.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* A credit card is essentially a means of borrowing money that is accompanied by interest and sometimes fees.
3-
* Validation is based on Luhn algorithm
3+
* Validation is based on Luhn algorithm
44
* @param cardNumber
55
* @returns
66
*/
@@ -9,7 +9,7 @@ export const isValid_Credit_Card = (cardNumber: string): boolean => {
99
let sum = 0;
1010
let shouldDouble = false;
1111

12-
if(!sanitized) return false;
12+
if (!sanitized) return false;
1313

1414
for (let i = sanitized.length - 1; i >= 0; i--) {
1515
let digit = parseInt(sanitized[i], 10);

src/ip.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Validates an IPv4 address.
3+
* @param ip - The IPv4 address to validate.
4+
* @returns True if the IPv4 address is valid, otherwise false.
5+
*/
6+
export const isValid_IPv4 = (ip: string): boolean => {
7+
const ipv4Regex =
8+
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
9+
return ipv4Regex.test(ip);
10+
};
11+
12+
/**
13+
* Validates an IPv6 address.
14+
* @param ip - The IPv6 address to validate.
15+
* @returns True if the IPv6 address is valid, otherwise false.
16+
*/
17+
export const isValid_IPv6 = (ip: string): boolean => {
18+
const ipv6Regex =
19+
/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]|)[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]|)[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]|)[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]|)[0-9]))$/;
20+
return ipv6Regex.test(ip);
21+
};

0 commit comments

Comments
 (0)