Validator.js: Add support for IPv6 in isIPRange(str)

Created on 1 Jan 2021  路  3Comments  路  Source: validatorjs/validator.js

Describe the bug
Currently, the isIPRange() method only supports IPv4 addresses. It should be extended to support IPv6 addresses as well.

Examples

isIPRange("192.0.2.0/24") // IPv4 CIDR is `true`
isIPRange("2001:db8::/32") // IPv6 CIDR is `false`

Additional context

馃悰 bug

Most helpful comment

@AnandChowdhary I opened a PR. after they review, it can be merged

All 3 comments

I suppose "isIPRange" check the string before '/' is valid IP or not. Maybe you should use valid ipv6 instead of "2001:db8::"

edit: sorry i've checked the code here the return statement of isIPRange

return isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;

so it validate only IPv4. It may be rewritten as

return (isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0) || (isIP(parts[0],6) && parts[1] <= 64 && parts[1] >= 0);

You can open PR.

@AnandChowdhary I opened a PR. after they review, it can be merged

This PR should resolve this issue: https://github.com/validatorjs/validator.js/pull/1594

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaxkodex picture jaxkodex  路  3Comments

woverton picture woverton  路  4Comments

frontendmonster picture frontendmonster  路  4Comments

rathboma picture rathboma  路  4Comments

spyshower picture spyshower  路  3Comments