validate

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And added in v0.7.0

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

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

func IsAbsFilePath

func IsAbsFilePath(value string) error

IsAbsFilePath checks if value is an absolute file path.

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 architecture name.

func IsBool

func IsBool(value string) error

IsBool validates if string can be understood as a bool.

func IsCloudInitUserData

func IsCloudInitUserData(value string) error

IsCloudInitUserData checks value is valid cloud-init user data.

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 IsDeviceName

func IsDeviceName(name string) error

IsDeviceName checks name is 1-63 characters long, doesn't start with a full stop and contains only alphanumeric, forward slash, hyphen, colon, underscore and full stop characters.

func IsHostname

func IsHostname(name string) error

IsHostname checks the string is valid DNS hostname.

func IsInRange

func IsInRange(min int64, max int64) func(value string) error

IsInRange checks whether an integer is within a specific range.

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 IsListOf

func IsListOf(validator func(value string) error) func(value string) error

IsListOf returns a validator for a comma separated list of values.

func IsListenAddress

func IsListenAddress(allowDNS bool, allowWildcard bool, requirePort bool) func(value string) error

IsListenAddress returns a validator for a listen address.

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 address string in CIDR format.

func IsNetworkAddressCIDRV4

func IsNetworkAddressCIDRV4(value string) error

IsNetworkAddressCIDRV4 validates an IPv4 address string in CIDR format.

func IsNetworkAddressCIDRV6

func IsNetworkAddressCIDRV6(value string) error

IsNetworkAddressCIDRV6 validates an IPv6 address string in CIDR format.

func IsNetworkAddressV4

func IsNetworkAddressV4(value string) error

IsNetworkAddressV4 validates an IPv4 address string.

func IsNetworkAddressV6

func IsNetworkAddressV6(value string) error

IsNetworkAddressV6 validates an IPv6 address string.

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/incus/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 IsNetworkName

func IsNetworkName(value string) error

IsNetworkName validates a name usable for a network.

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 "port" or "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 IsNetworkRangeV6

func IsNetworkRangeV6(value string) error

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

func IsNetworkV4

func IsNetworkV4(value string) error

IsNetworkV4 validates an IPv4 CIDR string.

func IsNetworkV6

func IsNetworkV6(value string) error

IsNetworkV6 validates an IPv6 CIDR string.

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(valid ...string) func(value 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/incus/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 IsRequestURL

func IsRequestURL(value string) error

IsRequestURL checks value is a valid HTTP/HTTPS request URL.

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 IsUint32Range

func IsUint32Range(value string) error

IsUint32Range validates whether the string is a uint32 range in the form "number" or "start-end".

func IsUint8

func IsUint8(value string) error

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

func IsValidCPUSet

func IsValidCPUSet(value string) error

IsValidCPUSet checks value is a valid CPU set.

Example
package main

import (
	"fmt"

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

func main() {
	tests := []string{
		"1",       // valid
		"1,2,3",   // valid
		"1-3",     // valid
		"1-3,4-6", // valid
		"1-3,4",   // valid
		"abc",     // invalid syntax
		"1-",      // invalid syntax
		"1,",      // invalid syntax
		"-1",      // invalid syntax
		",1",      // invalid syntax
		"1,2,3,3", // invalid: Duplicate CPU
		"1-2,2",   // invalid: Duplicate CPU
		"1-2,2-3", // invalid: Duplicate CPU
	}

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

}
Output:

<nil>
<nil>
<nil>
<nil>
<nil>
Invalid CPU limit syntax
Invalid CPU limit syntax
Invalid CPU limit syntax
Invalid CPU limit syntax
Invalid CPU limit syntax
Cannot define CPU multiple times
Cannot define CPU multiple times
Cannot define CPU multiple times

func IsYAML

func IsYAML(value string) error

IsYAML checks value is valid YAML.

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/incus/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 Or added in v0.7.0

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

Or returns a function that runs one or more validators, at least one must pass without error.

func ParseNetworkVLANRange

func ParseNetworkVLANRange(vlan string) (int, int, error)

ParseNetworkVLANRange parses a VLAN range in the form "number" or "start-end". Returns the start number and the number of items in the range.

func Required

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

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

Example
package main

import (
	"fmt"

	"github.com/lxc/incus/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