beaugunderson/ip-address

isValid not working for IPv4 subnets

IliaVolk opened this issue · 1 comments

const Address4 = require('ip-address').Address4;
const address = new Address4('47.227.219.97/16');
console.log(address.isValid());

outputs true
but should false, like python's similar library

Python 3.5.6 (default, Nov 13 2018, 08:43:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.ip_network('47.227.219.97/24')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/alt/python35/lib64/python3.5/ipaddress.py", line 74, in ip_network
    return IPv4Network(address, strict)
  File "/opt/alt/python35/lib64/python3.5/ipaddress.py", line 1537, in __init__
    raise ValueError('%s has host bits set' % self)
ValueError: 47.227.219.97/24 has host bits set

I think it's confusion on your part between IP inside of subnet and actual sub network address.

Here is startAddress() doc fragment: The first address in the range given by this address' subnet. Often referred to as the Network Address.

So if you'd like to check whether given IP is a valid network cidr you perhaps should compare startAddress() and correctForm().

Overall this lib's constructor behaves close to Python's

ipaddress.ip_network('47.227.219.97/24', False)

only keeping original IP around (which Python's ipaddress does not)