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

isValid not working for IPv4 subnets #81

Open
IliaVolk opened this issue Jan 9, 2019 · 1 comment
Open

isValid not working for IPv4 subnets #81

IliaVolk opened this issue Jan 9, 2019 · 1 comment

Comments

@IliaVolk
Copy link

IliaVolk commented Jan 9, 2019

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
@asergeyev
Copy link

asergeyev commented Apr 6, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants