validator

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package validator provides common validation helpers commonly used in operations tools. Additionally structures can be marked up with tags indicating the validation of individual keys and the entire struct can be validated in one go

Example (Enum)
package main

import (
	"fmt"

	"github.com/choria-io/go-choria/validator/enum"
)

func main() {
	valid := []string{"one", "two", "three"}

	ok, err := enum.ValidateSlice([]string{"one", "two"}, valid)
	if !ok {
		panic(err)
	}

	fmt.Println("slice 1 is valid")

	ok, _ = enum.ValidateSlice([]string{"5", "6"}, valid)
	if !ok {
		fmt.Println("slice 2 is invalid")
	}

	// string is valid
	ok, err = enum.ValidateString("one", valid)
	if !ok {
		panic(err)
	}

	fmt.Println("string is valid")

}
Output:

slice 1 is valid
slice 2 is invalid
string is valid
Example (Maxlength)
package main

import (
	"fmt"

	"github.com/choria-io/go-choria/validator/maxlength"
)

func main() {
	ok, err := maxlength.ValidateString("a short string", 20)
	if !ok {
		panic(err)
	}

	fmt.Println("string validates")

}
Output:

string validates
Example (Shellsafe)
package main

import (
	"fmt"

	"github.com/choria-io/go-choria/validator/shellsafe"
)

func main() {
	// a sell safe command, unsafe might be `un > safe`
	ok, err := shellsafe.Validate("safe")
	if !ok {
		panic(err)
	}

	fmt.Printf("string is safe")
}
Output:

string is safe
Example (Struct)
package main

import (
	"fmt"

	validator "github.com/choria-io/go-choria/validator"
)

type Request struct {
	Command string   `validate:"shellsafe"`
	Flags   []string `validate:"enum=debug,verbose"`
	Args    string   `validate:"maxlength=128"`
	AnyIP   string   `validate:"ipaddress"` // can also check ipv4 and ipv6
	User    string   `validate:"regex=^\\w+$"`
}

func main() {
	r := Request{
		Command: "/bin/some/command",
		Flags:   []string{"debug"},
		Args:    "hello world",
		AnyIP:   "2a00:1450:4003:807::200e",
		User:    "bob",
	}

	ok, err := validator.ValidateStruct(r)
	if !ok {
		panic(err)
	}

	fmt.Println("valid request")

	ok, err = validator.ValidateStructField(r, "Command")
	if !ok {
		panic(err)
	}

	fmt.Println("valid field")

}
Output:

valid request
valid field

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAnyFloat added in v0.19.0

func IsAnyFloat(i any) bool

IsAnyFloat determines if i is a float32 or float64

func IsAnyInt added in v0.19.0

func IsAnyInt(i any) bool

IsAnyInt determines if i is a int, int8, int16, in32 or int64

func IsArray added in v0.19.0

func IsArray(i any) bool

IsArray determines if i is a slice or array

func IsBool added in v0.19.0

func IsBool(i any) bool

IsBool determines if i is a boolean

func IsFloat32 added in v0.19.0

func IsFloat32(i any) bool

IsFloat32 determines if i is a float32

func IsFloat64 added in v0.19.0

func IsFloat64(i any) bool

IsFloat64 determines if i is a float64

func IsInt added in v0.19.0

func IsInt(i any) bool

IsInt determines if i is a int

func IsInt16 added in v0.19.0

func IsInt16(i any) bool

IsInt16 determines if i is a int16

func IsInt32 added in v0.19.0

func IsInt32(i any) bool

IsInt32 determines if i is a int32

func IsInt64 added in v0.19.0

func IsInt64(i any) bool

IsInt64 determines if i is a int64

func IsInt8 added in v0.19.0

func IsInt8(i any) bool

IsInt8 determines if i is a int8

func IsIntFloat64 added in v0.21.0

func IsIntFloat64(i any) bool

IsIntFloat64 checks if a float64 is a whole integer, important when comparing data from JSON Unmarshal that's always float64 if an interface

func IsMap added in v0.19.0

func IsMap(i any) bool

IsMap determines if i is a map

func IsNumber added in v0.19.0

func IsNumber(i any) bool

IsNumber determines if i is a int or a float of any size

func IsString added in v0.19.0

func IsString(i any) bool

IsString determines if i is a string

func ValidateStruct

func ValidateStruct(target any) (bool, error)

ValidateStruct validates all keys in a struct using their validate tag

func ValidateStructField

func ValidateStructField(target any, field string) (bool, error)

ValidateStructField validates one field in a struct

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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