Skip to content

Commit ac3d50a

Browse files
feat(isIP): allow usage of options object (#2089)
* refactor: allow for splitting tests to different files * feat(isAfter): allow usage of options object * style: make options italic * refactor: rename test file extension to .test.js * refactor: rename test-functions to testFunctions * refactor: implement suggestion from #2019 review * refactor: remove custom repeat to use native function * refactor: implement suggestion new Date * Refactor isIP with options API * Move and fix tests * Add more tests * Fix README * Fix broken isIPRange * refactor: change test files to prepare for #1874 * Maintain legacy syntax * Revert "Fix broken isIPRange" This reverts commit cf9ebc7. * Improve naming * Fix comment text * Reinstate test for isAlpha --------- Co-authored-by: Rik Smale <[email protected]>
1 parent f5da7fb commit ac3d50a

File tree

4 files changed

+319
-139
lines changed

4 files changed

+319
-139
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Validator | Description
123123
**isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. IMEI should be of format `###############` or `##-######-######-#`.<br/><br/>`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format. If `allow_hyphens` is set to true, the validator will validate the second format.
124124
**isIn(str, values)** | check if the string is in an array of allowed values.
125125
**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).
126-
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
126+
**isIP(str [, options])** | check if the string is an IP address (version 4 or 6).<br/><br/>`options` is an object that defaults to `{ version: '' }`.<br/><br/>**Options:**<br/>`version`: defines which IP version to compare to. Accepted values: `4`, `6`, `'4'`, `'6'`.
127127
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
128128
**isISBN(str [, options])** | check if the string is an [ISBN][ISBN].<br/><br/>`options` is an object that has no default.<br/>**Options:**<br/>`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested.
129129
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).

src/lib/isIP.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,24 @@ const IPv6AddressRegExp = new RegExp('^(' +
4444
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` +
4545
')(%[0-9a-zA-Z-.:]{1,})?$');
4646

47-
export default function isIP(str, version = '') {
48-
assertString(str);
49-
version = String(version);
47+
export default function isIP(ipAddress, options = {}) {
48+
assertString(ipAddress);
49+
50+
// accessing 'arguments' for backwards compatibility: isIP(ipAddress [, version])
51+
// eslint-disable-next-line prefer-rest-params
52+
const version = (typeof options === 'object' ? options.version : arguments[1]) || '';
53+
5054
if (!version) {
51-
return isIP(str, 4) || isIP(str, 6);
55+
return isIP(ipAddress, { version: 4 }) || isIP(ipAddress, { version: 6 });
5256
}
53-
if (version === '4') {
54-
return IPv4AddressRegExp.test(str);
57+
58+
if (version.toString() === '4') {
59+
return IPv4AddressRegExp.test(ipAddress);
5560
}
56-
if (version === '6') {
57-
return IPv6AddressRegExp.test(str);
61+
62+
if (version.toString() === '6') {
63+
return IPv6AddressRegExp.test(ipAddress);
5864
}
65+
5966
return false;
6067
}

test/validators.test.js

Lines changed: 1 addition & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,136 +1047,6 @@ describe('Validators', () => {
10471047
});
10481048
});
10491049

1050-
it('should validate IP addresses', () => {
1051-
test({
1052-
validator: 'isIP',
1053-
valid: [
1054-
'127.0.0.1',
1055-
'0.0.0.0',
1056-
'255.255.255.255',
1057-
'1.2.3.4',
1058-
'::1',
1059-
'2001:db8:0000:1:1:1:1:1',
1060-
'2001:db8:3:4::192.0.2.33',
1061-
'2001:41d0:2:a141::1',
1062-
'::ffff:127.0.0.1',
1063-
'::0000',
1064-
'0000::',
1065-
'1::',
1066-
'1111:1:1:1:1:1:1:1',
1067-
'fe80::a6db:30ff:fe98:e946',
1068-
'::',
1069-
'::8',
1070-
'::ffff:127.0.0.1',
1071-
'::ffff:255.255.255.255',
1072-
'::ffff:0:255.255.255.255',
1073-
'::2:3:4:5:6:7:8',
1074-
'::255.255.255.255',
1075-
'0:0:0:0:0:ffff:127.0.0.1',
1076-
'1:2:3:4:5:6:7::',
1077-
'1:2:3:4:5:6::8',
1078-
'1::7:8',
1079-
'1:2:3:4:5::7:8',
1080-
'1:2:3:4:5::8',
1081-
'1::6:7:8',
1082-
'1:2:3:4::6:7:8',
1083-
'1:2:3:4::8',
1084-
'1::5:6:7:8',
1085-
'1:2:3::5:6:7:8',
1086-
'1:2:3::8',
1087-
'1::4:5:6:7:8',
1088-
'1:2::4:5:6:7:8',
1089-
'1:2::8',
1090-
'1::3:4:5:6:7:8',
1091-
'1::8',
1092-
'fe80::7:8%eth0',
1093-
'fe80::7:8%1',
1094-
'64:ff9b::192.0.2.33',
1095-
'0:0:0:0:0:0:10.0.0.1',
1096-
],
1097-
invalid: [
1098-
'abc',
1099-
'256.0.0.0',
1100-
'0.0.0.256',
1101-
'26.0.0.256',
1102-
'0200.200.200.200',
1103-
'200.0200.200.200',
1104-
'200.200.0200.200',
1105-
'200.200.200.0200',
1106-
'::banana',
1107-
'banana::',
1108-
'::1banana',
1109-
'::1::',
1110-
'1:',
1111-
':1',
1112-
':1:1:1::2',
1113-
'1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1',
1114-
'::11111',
1115-
'11111:1:1:1:1:1:1:1',
1116-
'2001:db8:0000:1:1:1:1::1',
1117-
'0:0:0:0:0:0:ffff:127.0.0.1',
1118-
'0:0:0:0:ffff:127.0.0.1',
1119-
],
1120-
});
1121-
test({
1122-
validator: 'isIP',
1123-
args: [4],
1124-
valid: [
1125-
'127.0.0.1',
1126-
'0.0.0.0',
1127-
'255.255.255.255',
1128-
'1.2.3.4',
1129-
'255.0.0.1',
1130-
'0.0.1.1',
1131-
],
1132-
invalid: [
1133-
'::1',
1134-
'2001:db8:0000:1:1:1:1:1',
1135-
'::ffff:127.0.0.1',
1136-
'137.132.10.01',
1137-
'0.256.0.256',
1138-
'255.256.255.256',
1139-
],
1140-
});
1141-
test({
1142-
validator: 'isIP',
1143-
args: [6],
1144-
valid: [
1145-
'::1',
1146-
'2001:db8:0000:1:1:1:1:1',
1147-
'::ffff:127.0.0.1',
1148-
'fe80::1234%1',
1149-
'ff08::9abc%10',
1150-
'ff08::9abc%interface10',
1151-
'ff02::5678%pvc1.3',
1152-
],
1153-
invalid: [
1154-
'127.0.0.1',
1155-
'0.0.0.0',
1156-
'255.255.255.255',
1157-
'1.2.3.4',
1158-
'::ffff:287.0.0.1',
1159-
'%',
1160-
'fe80::1234%',
1161-
'fe80::1234%1%3%4',
1162-
'fe80%fe80%',
1163-
],
1164-
});
1165-
test({
1166-
validator: 'isIP',
1167-
args: [10],
1168-
valid: [],
1169-
invalid: [
1170-
'127.0.0.1',
1171-
'0.0.0.0',
1172-
'255.255.255.255',
1173-
'1.2.3.4',
1174-
'::1',
1175-
'2001:db8:0000:1:1:1:1:1',
1176-
],
1177-
});
1178-
});
1179-
11801050
it('should validate isIPRange', () => {
11811051
test({
11821052
validator: 'isIPRange',
@@ -1371,6 +1241,7 @@ describe('Validators', () => {
13711241
],
13721242
});
13731243
});
1244+
13741245
it('should validate alpha strings', () => {
13751246
test({
13761247
validator: 'isAlpha',

0 commit comments

Comments
 (0)