MCPcopy
hub / github.com/validatorjs/validator.js / isIPRange

Function isIPRange

src/lib/isIPRange.js:8–47  ·  view source on GitHub ↗
(str, version = '')

Source from the content-addressed store, hash-verified

6const v6Subnet = 128;
7
8export default function isIPRange(str, version = '') {
9 assertString(str);
10 const parts = str.split('/');
11
12 // parts[0] -> ip, parts[1] -> subnet
13 if (parts.length !== 2) {
14 return false;
15 }
16
17 if (!subnetMaybe.test(parts[1])) {
18 return false;
19 }
20
21 // Disallow preceding 0 i.e. 01, 02, ...
22 if (parts[1].length > 1 && parts[1].startsWith('0')) {
23 return false;
24 }
25
26 const isValidIP = isIP(parts[0], version);
27 if (!isValidIP) {
28 return false;
29 }
30
31 // Define valid subnet according to IP's version
32 let expectedSubnet = null;
33 switch (String(version)) {
34 case '4':
35 expectedSubnet = v4Subnet;
36 break;
37
38 case '6':
39 expectedSubnet = v6Subnet;
40 break;
41
42 default:
43 expectedSubnet = isIP(parts[0], '6') ? v6Subnet : v4Subnet;
44 }
45
46 return parts[1] <= expectedSubnet && parts[1] >= 0;
47}

Callers

nothing calls this directly

Calls 2

assertStringFunction · 0.85
isIPFunction · 0.85

Tested by

no test coverage detected