Skip to content

isValid not working for IPv4 subnets #81

Closed

Description

@IliaVolk
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

Activity

  1. asergeyev commented on Apr 6, 2021

    @asergeyev

    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)

  2. beaugunderson commented on May 1, 2026

    @beaugunderson
    Owner

    improved the docs via 98927be to make this clearer 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions