This Commented CUE
demonstrates how to use the built-in function
net.IP
to check that values represent valid IPv4 or IPv6 addresses as strings or lists
of bytes.
file.cue
package example
import "net"
// All top-level fields must represent valid IP addresses.
[_]: net.IP
// These fields represent the same valid IPv4 address.
v4String: "198.51.100.14"
v4Bytes: [198, 51, 100, 14]
// These fields represent the same valid IPv6 address.
v6StringFull: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
v6StringShort: "2001:0db8:85a3::8a2e:0370:7334"
v6Bytes: [32, 1, 13, 184, 133, 163, 0, 0, 1, 0, 138, 46, 3, 112, 115, 52]
// Some invalid IP addresses.
v4TooManyOctets: "198.51.100.14.0"
v4OctetTooLarge: [300, 51, 100, 14]
v6ByteTooLarge: [300, 1, 13, 184, 133, 163, 0, 0, 1, 0, 138, 46, 3, 112, 115, 52]
TERMINAL
$ cue vet
v4OctetTooLarge: invalid value [300,51,100,14] (does not satisfy net.IP):
./file.cue:6:6
./file.cue:19:18
v4TooManyOctets: invalid value "198.51.100.14.0" (does not satisfy net.IP):
./file.cue:6:6
./file.cue:18:18
v6ByteTooLarge: invalid value [300,1,13,184,133,163,0,0,1,0,138,46,3,112,115,52] (does not satisfy net.IP):
./file.cue:6:6
./file.cue:20:17
Related content
- The
net
built-in package - How-to Guide: Using "net.IPv4" to validate IPv4 addresses
- How-to Guide: Using "net.IPCIDR" to validate IP CIDR ranges