This Commented CUE
demonstrates how to use the built-in function
net.IPv4
to check that values represent valid IPv4 addresses as strings or lists of
bytes.
file.cue
package example
import "net"
// All top-level fields must represent valid IPv4 addresses.
[_]: net.IPv4
// These fields represent the same valid IPv4 address.
v4String: "198.51.100.14"
v4Bytes: [198, 51, 100, 14]
// Some invalid IPv4 addresses.
tooManyOctets: "198.51.100.14.0"
octetTooLarge: [300, 51, 100, 14]
v6NotV4: "2001:0db8:85a3::8a2e:0370:7334"
TERMINAL
$ cue vet
octetTooLarge: invalid value [300,51,100,14] (does not satisfy net.IPv4):
./file.cue:6:6
./file.cue:14:16
tooManyOctets: invalid value "198.51.100.14.0" (does not satisfy net.IPv4):
./file.cue:6:6
./file.cue:13:16
v6NotV4: invalid value "2001:0db8:85a3::8a2e:0370:7334" (does not satisfy net.IPv4):
./file.cue:6:6
./file.cue:15:10
Related content
- The
net
built-in package - How-to Guide: Using "net.IP" to validate IP addresses
- How-to Guide: Using "net.IPCIDR" to validate IP CIDR ranges