Functional Weave
Code in Python

net.device-validate

Validate one network inventory entry: name, host, ports, optional MAC and subnet, with every problem listed.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 17 tests, run in TypeScript, Python and Rust.

What it does

Checks one entry of a network inventory file (the list of devices a monitor probes) and reports every problem with it at once, instead of the first one. An entry looks like this:

{ "name": "core-switch", "host": "10.0.0.2", "ports": [22, 443], "mac": "AA-BB-CC-DD-EE-FF", "subnet": "10.0.0.0/24" }

For example

  • validate_device(name router, host 192.168.1.1, ports 22, 80) → valid true, errors , name router, host 192.168.1.1, host kind ipv4, ports 22, 80, mac —, subnet — a minimal IPv4 entry is valid and passed through
  • validate_device(name core switch , host 10.0.0.2, ports 22, mac AA-BB-CC-DD-EE-FF, subnet 10.0.0.77/24) → valid true, errors , name core switch, host 10.0.0.2, host kind ipv4, ports 22, mac aa:bb:cc:dd:ee:ff, subnet 10.0.0.0/24 name is trimmed, MAC made canonical, subnet host bits cleared
  • validate_device(name v6, host 2001:DB8:0:0:0:0:0:1, ports 443, subnet 2001:db8::ffff/32) → valid true, errors , name v6, host 2001:db8::1, host kind ipv6, ports 443, mac —, subnet 2001:db8::/32 an IPv6 host and subnet are written in RFC 5952 form

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def validate_device(entry: Mapping[str, Any]) -> DeviceValidation
entryrecord{name, host, ports, mac?, subnet?} as read from an inventory file
returnsDeviceValidation

The type it declares, generated into your project

@dataclass(frozen=True)
class DeviceValidation:
    """An inventory entry checked field by field, normalised where valid."""

    valid: bool
    #: every problem, in field order; empty when valid
    errors: List[str]
    name: Optional[str]
    #: IPv4 as given, IPv6 canonical, host name lower-case
    host: Optional[str]
    #: ipv4, ipv6 or hostname
    host_kind: Optional[str]
    #: the valid ports, in order
    ports: List[int]
    #: canonical
    mac: Optional[str]
    #: canonical block
    subnet: Optional[str]

Your code names it in one line, in the file that uses it

from fune.net.device_validate import validate_device  # net.device-validate@^1
impl/python.py · 132 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Any, List, Mapping, Optional

from .net_cidr import cidr_info, is_cidr  ← from net.cidr ^1.0.0 · built alongside by fune
from .net_device_validate_types import DeviceValidation
from .net_hostname_validate import validate_hostname  ← from net.hostname-validate ^1.0.0 · built alongside by fune
from .net_ipv4 import is_ipv4  ← from net.ipv4 ^1.0.0 · built alongside by fune
from .net_ipv6 import format_ipv6, is_ipv6, parse_ipv6  ← from net.ipv6 ^1.0.0 · built alongside by fune
from .net_mac_address import validate_mac_address  ← from net.mac-address ^1.0.0 · built alongside by fune
from .net_port import is_port  ← from net.port ^1.0.0 · built alongside by fune

KNOWN = ("name", "host", "ports", "mac", "subnet")


def _present(entry: Mapping[str, Any], key: str) -> bool:
    return key in entry and entry[key] is not None


def _whole_number(value: Any) -> Optional[int]:
    """A whole number, whichever way the JSON reader stored it (22 and 22.0 are the same port)."""
    if isinstance(value, bool):
        return None
    if isinstance(value, int):
        return value
    if isinstance(value, float) and value == value and value not in (float("inf"), float("-inf")) and value.is_integer():
        return int(value)
    return None


def _canonical_ipv6_block(text: str) -> str:
    """An IPv6 block with its host bits cleared and its address in RFC 5952 form."""
    address, _, prefix_text = text.partition("/")
    prefix = int(prefix_text)
    groups = []
    for i, g in enumerate(parse_ipv6(address)):
        bits = min(16, max(0, prefix - 16 * i))
        groups.append(0 if bits == 0 else g & ((0xFFFF << (16 - bits)) & 0xFFFF))
    return "%s/%d" % (format_ipv6(groups), prefix)


def validate_device(entry: Mapping[str, Any]) -> DeviceValidation:
    """Check one inventory entry and say everything that is wrong with it at once,
    so a person fixing a file does not play whack-a-mole one error per run.
    Never raises: a bad entry is a result, not a crash.
    """
    errors: List[str] = []

    name: Optional[str] = None
    if not _present(entry, "name"):
        errors.append("name is required")
    elif not isinstance(entry["name"], str):
        errors.append("name must be text")
    elif entry["name"].strip(" \t\n\r\f\v") == "":
        errors.append("name must not be empty")
    else:
        name = entry["name"].strip(" \t\n\r\f\v")

    host: Optional[str] = None
    host_kind: Optional[str] = None
    if not _present(entry, "host"):
        errors.append("host is required")
    elif not isinstance(entry["host"], str):
        errors.append("host must be text")
    elif is_ipv4(entry["host"]):
        host = entry["host"]
        host_kind = "ipv4"
    elif is_ipv6(entry["host"]):
        host = format_ipv6(parse_ipv6(entry["host"]))
        host_kind = "ipv6"
    else:
        check = validate_hostname(entry["host"])
        if check.valid:
            host = check.hostname
            host_kind = "hostname"
        else:
            errors.append('host "%s" is not an IPv4 address, IPv6 address or host name: %s' % (entry["host"], check.reason))

    ports: List[int] = []
    if not _present(entry, "ports"):
        errors.append("ports is required")
    elif not isinstance(entry["ports"], (list, tuple)):
        errors.append("ports must be a list")
    elif len(entry["ports"]) == 0:
        errors.append("ports must not be empty")
    else:
        for i, item in enumerate(entry["ports"]):
            port = _whole_number(item)
            if port is None:
                errors.append("ports[%d] must be a whole number" % i)
            elif not is_port(port):
                errors.append("ports[%d] %d is not a port (1-65535)" % (i, port))
            elif port in ports:
                errors.append("ports[%d] %d is listed twice" % (i, port))
            else:
                ports.append(port)

    mac: Optional[str] = None
    if _present(entry, "mac"):
        if not isinstance(entry["mac"], str):
            errors.append("mac must be text")
        else:
            check = validate_mac_address(entry["mac"])
            if check.valid:
                mac = check.canonical
            else:
                errors.append('mac "%s" is not valid: %s' % (entry["mac"], check.reason))

    subnet: Optional[str] = None
    if _present(entry, "subnet"):
        if not isinstance(entry["subnet"], str):
            errors.append("subnet must be text")
        elif not is_cidr(entry["subnet"]):
            errors.append('subnet "%s" is not a CIDR block' % entry["subnet"])
        elif ":" in entry["subnet"]:
            subnet = _canonical_ipv6_block(entry["subnet"])
        else:
            subnet = cidr_info(entry["subnet"]).cidr

    # Sorted, not in file order: JavaScript lists integer-like keys first, so
    # "file order" would differ between languages.
    for key in sorted(k for k in entry.keys() if k not in KNOWN):
        errors.append('unknown field "%s"' % key)

    return DeviceValidation(
        valid=len(errors) == 0,
        errors=errors,
        name=name,
        host=host,
        host_kind=host_kind,
        ports=ports,
        mac=mac,
        subnet=subnet,
    )

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 6 dependencies, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add net.device-validate
Download for Python net.device-validate-1.0.0-python.fune · 17,294 bytes sha256 a6e1b5d4f2370703931df5191b39014a90e362aad19b20c6e826db8a8691805c

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./net.device-validate-1.0.0-python.fune, or fetch it from a terminal with fune pull net.device-validate@1.0.0:python.

The whole function, every language, is one file too: net.device-validate-1.0.0.fune, 29,150 bytes, sha256 f74a06dc52d0c5f4093e13b770f7f2c0508d155be2bfb2ef9ebed0ff496ad420. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before net.device-validate

after — your function gets the result and the arguments, and returns the final result.

# fune: after net.device-validate

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

# fune: replace net.cidr in net.device-validate
# fune: replace net.hostname-validate in net.device-validate
# fune: replace net.ipv4 in net.device-validate
# fune: replace net.ipv6 in net.device-validate
# fune: replace net.mac-address in net.device-validate
# fune: replace net.port in net.device-validate

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show net.device-validate --steps.

# fune: step net.device-validate after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
a minimal IPv4 entry is valid and passed through name router, host 192.168.1.1, ports 22, 80 → valid true, errors , name router, host 192.168.1.1, host kind ipv4, ports 22, 80, mac —, subnet —
name is trimmed, MAC made canonical, subnet host bits cleared name core switch , host 10.0.0.2, ports 22, mac AA-BB-CC-DD-EE-FF, subnet 10.0.0.77/24 → valid true, errors , name core switch, host 10.0.0.2, host kind ipv4, ports 22, mac aa:bb:cc:dd:ee:ff, subnet 10.0.0.0/24
an IPv6 host and subnet are written in RFC 5952 form name v6, host 2001:DB8:0:0:0:0:0:1, ports 443, subnet 2001:db8::ffff/32 → valid true, errors , name v6, host 2001:db8::1, host kind ipv6, ports 443, mac —, subnet 2001:db8::/32
a host name is lower-cased and loses its trailing dot name nas, host NAS.Example.COM., ports 445, mac aabb.ccdd.eeff → valid true, errors , name nas, host nas.example.com, host kind hostname, ports 445, mac aa:bb:cc:dd:ee:ff, subnet —
null optional fields are the same as leaving them out name x, host x, ports 1, mac —, subnet — → valid true, errors , name x, host x, host kind hostname, ports 1, mac —, subnet —
an empty entry lists every required field → valid false, errors name is required, host is required, ports is required, name —, host —, host kind —, ports , mac —, subnet —
wrong types are named field by field name 5, host x, ports 22, mac 1, subnet true → valid false, errors name must be text, host must be text, ports must be a list, mac must be text, subnet must be text, name —, host —, host kind —, ports , mac —, subnet —
every bad port is reported with its index; 8080.0 is port 8080 name a, host a.example, ports 0, 22.5, 80, 443, 443, 65,536, 8,080, true → valid false, errors ports[0] 0 is not a port (1-65535), ports[1] must be a whole number, ports[2] must be a whole number, ports[4] 443 is listed twice, ports[5] 65536 is not a por…
an empty port list is refused name a, host 10.0.0.1, ports → valid false, errors ports must not be empty, name a, host 10.0.0.1, host kind ipv4, ports , mac —, subnet —
192.168.1.256 is neither an address nor a host name (numeric top-level label) name bad, host 192.168.1.256, ports 80 → valid false, errors host "192.168.1.256" is not an IPv4 address, IPv6 address or host name: top-level label is all numeric, name bad, host —, host kind —, ports 80, mac —, subnet —
Show the other 7 tests
CaseArgumentsExpected
a leading-zero quad is refused rather than read as octal or as a name name octal, host 010.0.0.1, ports 80 → valid false, errors host "010.0.0.1" is not an IPv4 address, IPv6 address or host name: top-level label is all numeric, name octal, host —, host kind —, ports 80, mac —, subnet —
a doubled dot is an empty label name p, host printer..office, ports 631 → valid false, errors host "printer..office" is not an IPv4 address, IPv6 address or host name: empty label, name p, host —, host kind —, ports 631, mac —, subnet —
a blank name and an empty host name , host , ports 22 → valid false, errors name must not be empty, host "" is not an IPv4 address, IPv6 address or host name: empty, name —, host —, host kind —, ports 22, mac —, subnet —
a trailing newline makes the host invalid name nl, host 10.0.0.1 , ports 22 → valid false, errors host "10.0.0.1 " is not an IPv4 address, IPv6 address or host name: character other than a letter, digit, hyphen or dot, name nl, host —, host kind —, ports 22…
a bad MAC and a bad subnet are both reported name m, host 10.1.1.1, ports 22, mac aa:bb:cc:dd:ee:gg, subnet 10.0.0.0/33 → valid false, errors mac "aa:bb:cc:dd:ee:gg" is not valid: not a hex digit, subnet "10.0.0.0/33" is not a CIDR block, name m, host 10.1.1.1, host kind ipv4, ports 22, mac —, subnet…
a subnet without a prefix is not a CIDR block name s, host 10.1.1.1, ports 22, subnet 10.0.0.0 → valid false, errors subnet "10.0.0.0" is not a CIDR block, name s, host 10.1.1.1, host kind ipv4, ports 22, mac —, subnet —
unknown fields are reported in sorted order after everything else zone a, name x, host x, ports 1, colour b → valid false, errors unknown field "colour", unknown field "zone", name x, host x, host kind hostname, ports 1, mac —, subnet —

More from the author

`name`, `host` and `ports` are required; `mac` and `subnet` are optional (null is the same as leaving them out). The function never throws: a bad entry is a result with `valid: false` and a list of `errors`, so a monitor can report it and carry on with the rest of the file.

## What each field must be

| field | rule | built on | |-------|------|----------| | `name` | text, not blank; ASCII whitespace around it is trimmed | | | `host` | an IPv4 address, an IPv6 address or an RFC 1123 host name, tried in that order | `net.ipv4`, `net.ipv6`, `net.hostname-validate` | | `ports` | a non-empty list of whole numbers 1-65535, each once | `net.port` | | `mac` | an EUI-48 MAC in any of the forms `net.mac-address` accepts | `net.mac-address` | | `subnet` | an IPv4 or IPv6 CIDR block | `net.cidr` |

Anything else in the entry is reported as `unknown field "x"`, which catches typos such as `"port"` for `"ports"`. Unknown fields are listed in sorted order after the field errors, because JavaScript orders integer-like keys first and "file order" would differ between languages.

## The normal forms returned

- `host`: an IPv4 address as given (it can only be valid in one spelling), IPv6 in RFC 5952 canonical form, a host name lower-cased without its trailing dot. `hostKind` says which of `ipv4`, `ipv6` or `hostname` it is. - `ports`: the valid ports in order, a repeated one only once. `22.0` is port 22, since a JSON reader may store it as a float. - `mac`: lower-case, colon-separated. - `subnet`: host bits cleared (`10.0.0.77/24` is `10.0.0.0/24`), the address of an IPv6 block in RFC 5952 form.

Fields that fail are null (or left out of `ports`) in the result, while the rest are still normalised, so a report can name the device whose MAC is bad.

## Edge cases

- `192.168.1.256` and `010.0.0.1` are not IPv4 addresses (out of range, and a leading zero that some parsers read as octal), and they are not host names either: a top-level label may not be all digits (RFC 3696 section 2). A naive check lets them through as names. - A trailing newline is not trimmed from `host`; it makes the entry invalid. - Subnet membership is not checked here: a host name has no address until it is resolved, which is I/O. Check it afterwards with `net.cidr`'s `cidrContains` on the resolved address.

Files

PathBytes
README.md2,654
impl/python.py4,887
impl/rust.rs6,750
impl/typescript.ts4,582
vectors.json5,958