Skip to content

Commit 5d6db63

Browse files
authored
feat(isIPRange): add support for IP version 4 or 6 (#1594)
Co-authored-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent a31c116 commit 5d6db63

3 files changed

Lines changed: 97 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Validator | Description
120120
**isIn(str, values)** | check if the string is in a array of allowed values.
121121
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
122122
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
123-
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
123+
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
124124
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
125125
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
126126
**isISO8601(str)** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date. <br/>`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid.

src/lib/isIPRange.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import assertString from './util/assertString';
22
import isIP from './isIP';
33

4-
const subnetMaybe = /^\d{1,2}$/;
4+
const subnetMaybe = /^\d{1,3}$/;
5+
const v4Subnet = 32;
6+
const v6Subnet = 128;
57

6-
export default function isIPRange(str) {
8+
export default function isIPRange(str, version = '') {
79
assertString(str);
810
const parts = str.split('/');
911

@@ -21,5 +23,25 @@ export default function isIPRange(str) {
2123
return false;
2224
}
2325

24-
return isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
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;
2547
}

test/validators.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,18 +872,88 @@ describe('Validators', () => {
872872
'127.0.0.1/24',
873873
'0.0.0.0/0',
874874
'255.255.255.0/32',
875+
'::/0',
876+
'::/128',
877+
'2001::/128',
878+
'2001:800::/128',
879+
'::ffff:127.0.0.1/128',
875880
],
876881
invalid: [
882+
'abc',
877883
'127.200.230.1/35',
878884
'127.200.230.1/-1',
879885
'1.1.1.1/011',
880-
'::1/64',
881886
'1.1.1/24.1',
882887
'1.1.1.1/01',
883888
'1.1.1.1/1.1',
884889
'1.1.1.1/1.',
885890
'1.1.1.1/1/1',
886891
'1.1.1.1',
892+
'::1',
893+
'::1/164',
894+
'2001::/240',
895+
'2001::/-1',
896+
'2001::/001',
897+
'2001::/24.1',
898+
'2001:db8:0000:1:1:1:1:1',
899+
'::ffff:127.0.0.1',
900+
],
901+
});
902+
test({
903+
validator: 'isIPRange',
904+
args: [4],
905+
valid: [
906+
'127.0.0.1/1',
907+
'0.0.0.0/1',
908+
'255.255.255.255/1',
909+
'1.2.3.4/1',
910+
'255.0.0.1/1',
911+
'0.0.1.1/1',
912+
],
913+
invalid: [
914+
'abc',
915+
'::1',
916+
'2001:db8:0000:1:1:1:1:1',
917+
'::ffff:127.0.0.1',
918+
'137.132.10.01',
919+
'0.256.0.256',
920+
'255.256.255.256',
921+
],
922+
});
923+
test({
924+
validator: 'isIPRange',
925+
args: [6],
926+
valid: [
927+
'::1/1',
928+
'2001:db8:0000:1:1:1:1:1/1',
929+
'::ffff:127.0.0.1/1',
930+
],
931+
invalid: [
932+
'abc',
933+
'127.0.0.1',
934+
'0.0.0.0',
935+
'255.255.255.255',
936+
'1.2.3.4',
937+
'::ffff:287.0.0.1',
938+
'::ffff:287.0.0.1/254',
939+
'%',
940+
'fe80::1234%',
941+
'fe80::1234%1%3%4',
942+
'fe80%fe80%',
943+
],
944+
});
945+
test({
946+
validator: 'isIPRange',
947+
args: [10],
948+
valid: [],
949+
invalid: [
950+
'abc',
951+
'127.0.0.1/1',
952+
'0.0.0.0/1',
953+
'255.255.255.255/1',
954+
'1.2.3.4/1',
955+
'::1/1',
956+
'2001:db8:0000:1:1:1:1:1/1',
887957
],
888958
});
889959
});

0 commit comments

Comments
 (0)