Metadata-Version: 2.5
Name: network-validators
Version: 0.1.0
Summary: Validators for IP addresses, subnets, ranges, hostnames, ports and MAC addresses, with stable error codes.
Project-URL: Homepage, https://github.com/icmp-type0/network-validators-py
Project-URL: Specification, https://github.com/icmp-type0/network-validators-py/blob/main/spec/SPEC.md
Project-URL: Issues, https://github.com/icmp-type0/network-validators-py/issues
License-Expression: MIT
License-File: LICENSE
Keywords: cidr,fqdn,hostname,ip,ipv4,ipv6,mac,port,subnet,validation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: System :: Networking
Classifier: Typing :: Typed
Requires-Python: >=3.11
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# network-validators

Validators for network-related input: IP addresses, subnets, IP ranges, hostnames, ports and MAC addresses. Each check returns a stable error code, not a message, so you write the messages your users see.

- No dependencies. Python 3.11+.
- Strict about common traps: IPv4 leading zeros (CVE-2021-29921), non-ASCII digits, surrounding whitespace, IPv6 zone IDs.
- Behaves the same as the [TypeScript package](https://www.npmjs.com/package/network-validators). Both follow one [specification](https://github.com/icmp-type0/network-validators-py/blob/main/spec/SPEC.md) and run the same test cases.

> Status: 0.1. Before 1.0, a minor version (0.2, 0.3, …) may include breaking changes. Every change is listed in the [changelog](https://github.com/icmp-type0/network-validators-py/blob/main/CHANGELOG.md).

## Install

```sh
pip install network-validators
```

## Usage

Every validator has two forms: `validate_*` returns a result, and `is_*` returns a `bool`.

```python
from network_validators import is_host, validate_subnet

r = validate_subnet("10.0.0.5/24", allow_host_bits=False)
r.valid    # False
r.code     # "subnet.host_bits_set"
r.details  # {"network": "10.0.0.0/24"}

if r:      # a result is truthy only when valid
    ...

is_host("example.com")  # True
```

Map codes to your own messages:

```python
from network_validators import ErrorCode, validate_subnet

MESSAGES: dict[ErrorCode, str] = {
    "empty": "This field is required.",
    "subnet.host_bits_set": "Did you mean {network}?",
    "port.out_of_range": "Port must be between {min} and {max}.",
}

r = validate_subnet(value, allow_host_bits=False)
if r.code is not None:  # same as `if not r`, and type checkers then see code as an ErrorCode
    message = MESSAGES.get(r.code, "Invalid value.").format(**r.details)
```

`ErrorCode` is a `Literal` type of every code, so a type checker flags a misspelled code in your map. `ERROR_CODES` lists them at runtime, for example to check that a message catalog covers them all.

Empty strings fail with code `empty`. If a field is optional, skip validation when it's empty.

Invalid options, such as `version=5`, an unknown option name or a bad entry in `subnets`, raise `TypeError` or `ValueError` instead of returning a result.

## Validators

| Function | Accepts | Options |
|---|---|---|
| `validate_ip_address` | `192.0.2.1`, `2001:db8::1` | `version`, `allow_zone_id`, `allow_unspecified`, `allow_private` |
| `validate_private_ip_address` | `10.0.0.1`, `fd00::1` | `version`, `allow_zone_id`, `additional_ranges` |
| `validate_netmask` | `255.255.255.0` | |
| `validate_subnet` | `10.0.0.0/8`, `10.0.0.0 255.0.0.0`, `2001:db8::/32` | `version`, `allow_netmask`, `allow_host_bits` |
| `validate_private_subnet` | `192.168.1.0/24` | as `validate_subnet`, plus `additional_ranges` |
| `validate_ip_range` | `10.0.0.1-10.0.0.50` | `version`, `allow_single` |
| `validate_ip_in_subnets` / `validate_ip_not_in_subnets` | an IP address | `subnets` (required), `version` |
| `validate_subnet_in_subnets` / `validate_subnet_not_overlapping` | a subnet | `subnets` (required), `version` |
| `validate_ip_range_not_overlapping` | an IP range | `subnets` (required), `version` |
| `validate_hostname` / `validate_fqdn` | `server1` / `www.example.com` | `allow_wildcard`, `allow_underscore`, `allow_trailing_dot` |
| `validate_host` | a hostname, FQDN or IP address | IP and name options, plus `allow_single_label` |
| `validate_port` / `validate_port_range` | `443` / `8000-8080` | `allow_zero` |
| `validate_port_list` | `80, 443, 8000-8080` | `allow_zero`, `allow_ranges` |
| `validate_mac_address` | `00:1a:2b:3c:4d:5e`, `001a.2b3c.4d5e` | `formats` |

`version` is `4`, `6` or `"any"` (the default).

The [specification](https://github.com/icmp-type0/network-validators-py/blob/main/spec/SPEC.md) gives each validator's exact rules and the order of its checks. All error codes are listed in [codes.json](https://github.com/icmp-type0/network-validators-py/blob/main/spec/codes.json).

## License

MIT
