Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isIP): allow usage of options object #2089

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `##-######-######-#`.<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.
**isIn(str, values)** | check if the string is in an array of allowed values.
**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).
**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).<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'`.
**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].<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.
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
Expand Down
23 changes: 15 additions & 8 deletions src/lib/isIP.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
131 changes: 1 addition & 130 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -1296,6 +1166,7 @@ describe('Validators', () => {
],
});
});

it('should validate alpha strings', () => {
test({
validator: 'isAlpha',
Expand Down
Loading