diff --git a/README.md b/README.md
index e4d1d7f4e..ae8218147 100644
--- a/README.md
+++ b/README.md
@@ -125,7 +125,7 @@ Validator | Description
**isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. IMEI should be of format `###############` or `##-######-######-#`.
`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.
**isIn(str, values)** | check if the string is in an array of allowed values.
**isInt(str [, options])** | check if the string is an integer.
`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).
-**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
+**isIP(str [, options])** | check if the string is an IP address (version 4 or 6).
`options` is an object that defaults to `{ version: '' }`.
**Options:**
`version`: defines which IP version to compare to. Accepted values: `4`, `6`, `'4'`, `'6'`.
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
**isISBN(str [, options])** | check if the string is an [ISBN][ISBN].
`options` is an object that has no default.
**Options:**
`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested.
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
diff --git a/src/lib/isIP.js b/src/lib/isIP.js
index 40ca19aec..dca52d2da 100644
--- a/src/lib/isIP.js
+++ b/src/lib/isIP.js
@@ -44,17 +44,24 @@ const IPv6AddressRegExp = new RegExp('^(' +
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` +
')(%[0-9a-zA-Z-.:]{1,})?$');
-export default function isIP(str, version = '') {
- assertString(str);
- version = String(version);
+export default function isIP(ipAddress, options = {}) {
+ assertString(ipAddress);
+
+ // accessing 'arguments' for backwards compatibility: isIP(ipAddress [, version])
+ // eslint-disable-next-line prefer-rest-params
+ const version = (typeof options === 'object' ? options.version : arguments[1]) || '';
+
if (!version) {
- return isIP(str, 4) || isIP(str, 6);
+ return isIP(ipAddress, { version: 4 }) || isIP(ipAddress, { version: 6 });
}
- if (version === '4') {
- return IPv4AddressRegExp.test(str);
+
+ if (version.toString() === '4') {
+ return IPv4AddressRegExp.test(ipAddress);
}
- if (version === '6') {
- return IPv6AddressRegExp.test(str);
+
+ if (version.toString() === '6') {
+ return IPv6AddressRegExp.test(ipAddress);
}
+
return false;
}
diff --git a/test/validators.test.js b/test/validators.test.js
index a1079b34f..eb8b1918d 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -972,136 +972,6 @@ describe('Validators', () => {
});
});
- it('should validate IP addresses', () => {
- test({
- validator: 'isIP',
- valid: [
- '127.0.0.1',
- '0.0.0.0',
- '255.255.255.255',
- '1.2.3.4',
- '::1',
- '2001:db8:0000:1:1:1:1:1',
- '2001:db8:3:4::192.0.2.33',
- '2001:41d0:2:a141::1',
- '::ffff:127.0.0.1',
- '::0000',
- '0000::',
- '1::',
- '1111:1:1:1:1:1:1:1',
- 'fe80::a6db:30ff:fe98:e946',
- '::',
- '::8',
- '::ffff:127.0.0.1',
- '::ffff:255.255.255.255',
- '::ffff:0:255.255.255.255',
- '::2:3:4:5:6:7:8',
- '::255.255.255.255',
- '0:0:0:0:0:ffff:127.0.0.1',
- '1:2:3:4:5:6:7::',
- '1:2:3:4:5:6::8',
- '1::7:8',
- '1:2:3:4:5::7:8',
- '1:2:3:4:5::8',
- '1::6:7:8',
- '1:2:3:4::6:7:8',
- '1:2:3:4::8',
- '1::5:6:7:8',
- '1:2:3::5:6:7:8',
- '1:2:3::8',
- '1::4:5:6:7:8',
- '1:2::4:5:6:7:8',
- '1:2::8',
- '1::3:4:5:6:7:8',
- '1::8',
- 'fe80::7:8%eth0',
- 'fe80::7:8%1',
- '64:ff9b::192.0.2.33',
- '0:0:0:0:0:0:10.0.0.1',
- ],
- invalid: [
- 'abc',
- '256.0.0.0',
- '0.0.0.256',
- '26.0.0.256',
- '0200.200.200.200',
- '200.0200.200.200',
- '200.200.0200.200',
- '200.200.200.0200',
- '::banana',
- 'banana::',
- '::1banana',
- '::1::',
- '1:',
- ':1',
- ':1:1:1::2',
- '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1',
- '::11111',
- '11111:1:1:1:1:1:1:1',
- '2001:db8:0000:1:1:1:1::1',
- '0:0:0:0:0:0:ffff:127.0.0.1',
- '0:0:0:0:ffff:127.0.0.1',
- ],
- });
- test({
- validator: 'isIP',
- args: [4],
- valid: [
- '127.0.0.1',
- '0.0.0.0',
- '255.255.255.255',
- '1.2.3.4',
- '255.0.0.1',
- '0.0.1.1',
- ],
- invalid: [
- '::1',
- '2001:db8:0000:1:1:1:1:1',
- '::ffff:127.0.0.1',
- '137.132.10.01',
- '0.256.0.256',
- '255.256.255.256',
- ],
- });
- test({
- validator: 'isIP',
- args: [6],
- valid: [
- '::1',
- '2001:db8:0000:1:1:1:1:1',
- '::ffff:127.0.0.1',
- 'fe80::1234%1',
- 'ff08::9abc%10',
- 'ff08::9abc%interface10',
- 'ff02::5678%pvc1.3',
- ],
- invalid: [
- '127.0.0.1',
- '0.0.0.0',
- '255.255.255.255',
- '1.2.3.4',
- '::ffff:287.0.0.1',
- '%',
- 'fe80::1234%',
- 'fe80::1234%1%3%4',
- 'fe80%fe80%',
- ],
- });
- test({
- validator: 'isIP',
- args: [10],
- valid: [],
- invalid: [
- '127.0.0.1',
- '0.0.0.0',
- '255.255.255.255',
- '1.2.3.4',
- '::1',
- '2001:db8:0000:1:1:1:1:1',
- ],
- });
- });
-
it('should validate isIPRange', () => {
test({
validator: 'isIPRange',
@@ -1296,6 +1166,7 @@ describe('Validators', () => {
],
});
});
+
it('should validate alpha strings', () => {
test({
validator: 'isAlpha',
diff --git a/test/validators/isIP.test.js b/test/validators/isIP.test.js
new file mode 100644
index 000000000..9b01d024f
--- /dev/null
+++ b/test/validators/isIP.test.js
@@ -0,0 +1,302 @@
+import test from '../testFunctions';
+
+describe('isIP', () => {
+ it('should validate IP addresses', () => {
+ test({
+ validator: 'isIP',
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '2001:db8:3:4::192.0.2.33',
+ '2001:41d0:2:a141::1',
+ '::ffff:127.0.0.1',
+ '::0000',
+ '0000::',
+ '1::',
+ '1111:1:1:1:1:1:1:1',
+ 'fe80::a6db:30ff:fe98:e946',
+ '::',
+ '::8',
+ '::ffff:127.0.0.1',
+ '::ffff:255.255.255.255',
+ '::ffff:0:255.255.255.255',
+ '::2:3:4:5:6:7:8',
+ '::255.255.255.255',
+ '0:0:0:0:0:ffff:127.0.0.1',
+ '1:2:3:4:5:6:7::',
+ '1:2:3:4:5:6::8',
+ '1::7:8',
+ '1:2:3:4:5::7:8',
+ '1:2:3:4:5::8',
+ '1::6:7:8',
+ '1:2:3:4::6:7:8',
+ '1:2:3:4::8',
+ '1::5:6:7:8',
+ '1:2:3::5:6:7:8',
+ '1:2:3::8',
+ '1::4:5:6:7:8',
+ '1:2::4:5:6:7:8',
+ '1:2::8',
+ '1::3:4:5:6:7:8',
+ '1::8',
+ 'fe80::7:8%eth0',
+ 'fe80::7:8%1',
+ '64:ff9b::192.0.2.33',
+ '0:0:0:0:0:0:10.0.0.1',
+ ],
+ invalid: [
+ 'abc',
+ '256.0.0.0',
+ '0.0.0.256',
+ '26.0.0.256',
+ '0200.200.200.200',
+ '200.0200.200.200',
+ '200.200.0200.200',
+ '200.200.200.0200',
+ '::banana',
+ 'banana::',
+ '::1banana',
+ '::1::',
+ '1:',
+ ':1',
+ ':1:1:1::2',
+ '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1',
+ '::11111',
+ '11111:1:1:1:1:1:1:1',
+ '2001:db8:0000:1:1:1:1::1',
+ '0:0:0:0:0:0:ffff:127.0.0.1',
+ '0:0:0:0:ffff:127.0.0.1',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: 'invalid version' }],
+ valid: [],
+ invalid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: null }],
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: undefined }],
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: 4 }],
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '255.0.0.1',
+ '0.0.1.1',
+ ],
+ invalid: [
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '::ffff:127.0.0.1',
+ '137.132.10.01',
+ '0.256.0.256',
+ '255.256.255.256',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: 6 }],
+ valid: [
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '::ffff:127.0.0.1',
+ 'fe80::1234%1',
+ 'ff08::9abc%10',
+ 'ff08::9abc%interface10',
+ 'ff02::5678%pvc1.3',
+ ],
+ invalid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::ffff:287.0.0.1',
+ '%',
+ 'fe80::1234%',
+ 'fe80::1234%1%3%4',
+ 'fe80%fe80%',
+ ],
+ });
+
+ test({
+ validator: 'isIP',
+ args: [{ version: 10 }],
+ valid: [],
+ invalid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ ],
+ });
+ });
+
+ describe('legacy syntax', () => {
+ it('should validate IP addresses', () => {
+ test({
+ validator: 'isIP',
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '2001:db8:3:4::192.0.2.33',
+ '2001:41d0:2:a141::1',
+ '::ffff:127.0.0.1',
+ '::0000',
+ '0000::',
+ '1::',
+ '1111:1:1:1:1:1:1:1',
+ 'fe80::a6db:30ff:fe98:e946',
+ '::',
+ '::8',
+ '::ffff:127.0.0.1',
+ '::ffff:255.255.255.255',
+ '::ffff:0:255.255.255.255',
+ '::2:3:4:5:6:7:8',
+ '::255.255.255.255',
+ '0:0:0:0:0:ffff:127.0.0.1',
+ '1:2:3:4:5:6:7::',
+ '1:2:3:4:5:6::8',
+ '1::7:8',
+ '1:2:3:4:5::7:8',
+ '1:2:3:4:5::8',
+ '1::6:7:8',
+ '1:2:3:4::6:7:8',
+ '1:2:3:4::8',
+ '1::5:6:7:8',
+ '1:2:3::5:6:7:8',
+ '1:2:3::8',
+ '1::4:5:6:7:8',
+ '1:2::4:5:6:7:8',
+ '1:2::8',
+ '1::3:4:5:6:7:8',
+ '1::8',
+ 'fe80::7:8%eth0',
+ 'fe80::7:8%1',
+ '64:ff9b::192.0.2.33',
+ '0:0:0:0:0:0:10.0.0.1',
+ ],
+ invalid: [
+ 'abc',
+ '256.0.0.0',
+ '0.0.0.256',
+ '26.0.0.256',
+ '0200.200.200.200',
+ '200.0200.200.200',
+ '200.200.0200.200',
+ '200.200.200.0200',
+ '::banana',
+ 'banana::',
+ '::1banana',
+ '::1::',
+ '1:',
+ ':1',
+ ':1:1:1::2',
+ '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1',
+ '::11111',
+ '11111:1:1:1:1:1:1:1',
+ '2001:db8:0000:1:1:1:1::1',
+ '0:0:0:0:0:0:ffff:127.0.0.1',
+ '0:0:0:0:ffff:127.0.0.1',
+ ],
+ });
+ test({
+ validator: 'isIP',
+ args: [4],
+ valid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '255.0.0.1',
+ '0.0.1.1',
+ ],
+ invalid: [
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '::ffff:127.0.0.1',
+ '137.132.10.01',
+ '0.256.0.256',
+ '255.256.255.256',
+ ],
+ });
+ test({
+ validator: 'isIP',
+ args: [6],
+ valid: [
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ '::ffff:127.0.0.1',
+ 'fe80::1234%1',
+ 'ff08::9abc%10',
+ 'ff08::9abc%interface10',
+ 'ff02::5678%pvc1.3',
+ ],
+ invalid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::ffff:287.0.0.1',
+ '%',
+ 'fe80::1234%',
+ 'fe80::1234%1%3%4',
+ 'fe80%fe80%',
+ ],
+ });
+ test({
+ validator: 'isIP',
+ args: [10],
+ valid: [],
+ invalid: [
+ '127.0.0.1',
+ '0.0.0.0',
+ '255.255.255.255',
+ '1.2.3.4',
+ '::1',
+ '2001:db8:0000:1:1:1:1:1',
+ ],
+ });
+ });
+ });
+});