validate

package
v0.0.0-...-c012b7a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAny

func IsAny(value string) error

IsAny accepts all strings as valid.

func IsArchitecture

func IsArchitecture(value string) error

IsArchitecture validates whether the value is a valid LXD architecture name.

func IsBool

func IsBool(value string) error

IsBool validates if string can be understood as a bool.

func IsCompressionAlgorithm

func IsCompressionAlgorithm(value string) error

IsCompressionAlgorithm validates whether a value is a valid compression algorithm and is available on the system.

func IsCron

func IsCron(aliases []string) func(value string) error

IsCron checks that it's a valid cron pattern or alias.

func IsDeviceID

func IsDeviceID(value string) error

IsDeviceID validates string is four lowercase hex characters suitable as Vendor or Device ID.

func IsInt64

func IsInt64(value string) error

IsInt64 validates whether the string can be converted to an int64.

func IsInterfaceName

func IsInterfaceName(value string) error

IsInterfaceName validates a real network interface name.

func IsNetwork

func IsNetwork(value string) error

IsNetwork validates an IP network CIDR string.

func IsNetworkAddress

func IsNetworkAddress(value string) error

IsNetworkAddress validates an IP (v4 or v6) address string.

func IsNetworkAddressCIDR

func IsNetworkAddressCIDR(value string) error

IsNetworkAddressCIDR validates an IP addresss string in CIDR format.

func IsNetworkAddressCIDRV4

func IsNetworkAddressCIDRV4(value string) error

IsNetworkAddressCIDRV4 validates an IPv4 addresss string in CIDR format.

func IsNetworkAddressCIDRV6

func IsNetworkAddressCIDRV6(value string) error

IsNetworkAddressCIDRV6 validates an IPv6 addresss string in CIDR format.

func IsNetworkAddressList

func IsNetworkAddressList(value string) error

IsNetworkAddressList validates a comma delimited list of IPv4 or IPv6 addresses.

func IsNetworkAddressV4

func IsNetworkAddressV4(value string) error

IsNetworkAddressV4 validates an IPv4 addresss string.

func IsNetworkAddressV4List

func IsNetworkAddressV4List(value string) error

IsNetworkAddressV4List validates a comma delimited list of IPv4 addresses.

func IsNetworkAddressV6

func IsNetworkAddressV6(value string) error

IsNetworkAddressV6 validates an IPv6 addresss string.

func IsNetworkAddressV6List

func IsNetworkAddressV6List(value string) error

IsNetworkAddressV6List validates a comma delimited list of IPv6 addresses.

func IsNetworkList

func IsNetworkList(value string) error

IsNetworkList validates a comma delimited list of IP network CIDR strings.

func IsNetworkMAC

func IsNetworkMAC(value string) error

IsNetworkMAC validates an Ethernet MAC address. e.g. "00:00:5e:00:53:01".

Example
package main

import (
	"fmt"

	"github.com/lxc/lxd/shared/validate"
)

func main() {
	tests := []string{
		"00:00:5e:00:53:01",
		"02:00:5e:10:00:00:00:01", // too long
		"00-00-5e-00-53-01",       // invalid delimiter
		"0000.5e00.5301",          // invalid delimiter
		"invalid",
		"",
	}

	for _, v := range tests {
		err := validate.IsNetworkMAC(v)
		fmt.Printf("%s, %t\n", v, err == nil)
	}

}
Output:

00:00:5e:00:53:01, true
02:00:5e:10:00:00:00:01, false
00-00-5e-00-53-01, false
0000.5e00.5301, false
invalid, false
, false

func IsNetworkMTU

func IsNetworkMTU(value string) error

IsNetworkMTU validates MTU number >= 1280 and <= 16384. Anything below 68 and the kernel doesn't allow IPv4, anything below 1280 and the kernel doesn't allow IPv6. So require an IPv6-compatible MTU as the low value and cap at the max ethernet jumbo frame size.

func IsNetworkPort

func IsNetworkPort(value string) error

IsNetworkPort validates an IP port number >= 0 and <= 65535.

func IsNetworkPortRange

func IsNetworkPortRange(value string) error

IsNetworkPortRange validates an IP port range in the format "start-end".

func IsNetworkRange

func IsNetworkRange(value string) error

IsNetworkRange validates an IP range in the format "start-end".

func IsNetworkRangeV4

func IsNetworkRangeV4(value string) error

IsNetworkRangeV4 validates an IPv4 range in the format "start-end".

func IsNetworkRangeV4List

func IsNetworkRangeV4List(value string) error

IsNetworkRangeV4List validates a comma delimited list of IPv4 ranges.

func IsNetworkRangeV6

func IsNetworkRangeV6(value string) error

IsNetworkRangeV6 validates an IPv6 range in the format "start-end".

func IsNetworkRangeV6List

func IsNetworkRangeV6List(value string) error

IsNetworkRangeV6List validates a comma delimited list of IPv6 ranges.

func IsNetworkV4

func IsNetworkV4(value string) error

IsNetworkV4 validates an IPv4 CIDR string.

func IsNetworkV4List

func IsNetworkV4List(value string) error

IsNetworkV4List validates a comma delimited list of IPv4 CIDR strings.

func IsNetworkV6

func IsNetworkV6(value string) error

IsNetworkV6 validates an IPv6 CIDR string.

func IsNetworkV6List

func IsNetworkV6List(value string) error

IsNetworkV6List validates a comma delimited list of IPv6 CIDR strings.

func IsNetworkVLAN

func IsNetworkVLAN(value string) error

IsNetworkVLAN validates a VLAN ID.

func IsNotEmpty

func IsNotEmpty(value string) error

IsNotEmpty requires a non-empty string.

func IsOneOf

func IsOneOf(value string, valid []string) error

IsOneOf checks whether the string is present in the supplied slice of strings.

func IsPCIAddress

func IsPCIAddress(value string) error

IsPCIAddress validates whether a value is a PCI address.

Example
package main

import (
	"fmt"

	"github.com/lxc/lxd/shared/validate"
)

func main() {
	tests := []string{
		"0000:12:ab.0", // valid
		"0010:12:ab.0", // valid
		"0000:12:CD.0", // valid
		"12:ab.0",      // valid
		"12:CD.0",      // valid
		"0000:12:gh.0", // invalid hex
		"0000:12:GH.0", // invalid hex
		"12:gh.0",      // invalid hex
		"12:GH.0",      // invalid hex
		"000:12:CD.0",  // wrong prefix
		"12.ab.0",      // invalid format
		"",
	}

	for _, v := range tests {
		err := validate.IsPCIAddress(v)
		fmt.Printf("%s, %t\n", v, err == nil)
	}

}
Output:

0000:12:ab.0, true
0010:12:ab.0, true
0000:12:CD.0, true
12:ab.0, true
12:CD.0, true
0000:12:gh.0, false
0000:12:GH.0, false
12:gh.0, false
12:GH.0, false
000:12:CD.0, false
12.ab.0, false
, false

func IsPriority

func IsPriority(value string) error

IsPriority validates priority number.

func IsSize

func IsSize(value string) error

IsSize checks if string is valid size according to units.ParseByteSizeString.

func IsURLSegmentSafe

func IsURLSegmentSafe(value string) error

IsURLSegmentSafe validates whether value can be used in a URL segment.

func IsUUID

func IsUUID(value string) error

IsUUID validates whether a value is a UUID.

func IsUint32

func IsUint32(value string) error

IsUint32 validates whether the string can be converted to an uint32.

func IsUint8

func IsUint8(value string) error

IsUint8 validates whether the string can be converted to an uint8.

func Optional

func Optional(validators ...func(value string) error) func(value string) error

Optional wraps Required() function to make it return nil if value is empty string.

Example
package main

import (
	"fmt"

	"github.com/lxc/lxd/shared/validate"
)

func main() {
	tests := []string{
		"",
		"foo",
		"true",
	}

	for _, v := range tests {
		f := validate.Optional()
		fmt.Printf("%v ", f(v))

		f = validate.Optional(validate.IsBool)
		fmt.Printf("%v\n", f(v))
	}

}
Output:

<nil> <nil>
<nil> Invalid value for a boolean "foo"
<nil> <nil>

func Required

func Required(validators ...func(value string) error) func(value string) error

Required returns function that runs one or more validators, all must pass without error.

Example
package main

import (
	"fmt"

	"github.com/lxc/lxd/shared/validate"
)

func main() {
	tests := []string{
		"",
		"foo",
		"true",
	}

	for _, v := range tests {
		f := validate.Required()
		fmt.Printf("%v ", f(v))

		f = validate.Required(validate.IsBool)
		fmt.Printf("%v\n", f(v))
	}

}
Output:

<nil> Invalid value for a boolean ""
<nil> Invalid value for a boolean "foo"
<nil> <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL