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 ¶
- func IsAnyFloat(i interface{}) bool
- func IsAnyInt(i interface{}) bool
- func IsArray(i interface{}) bool
- func IsBool(i interface{}) bool
- func IsFloat32(i interface{}) bool
- func IsFloat64(i interface{}) bool
- func IsInt(i interface{}) bool
- func IsInt16(i interface{}) bool
- func IsInt32(i interface{}) bool
- func IsInt64(i interface{}) bool
- func IsInt8(i interface{}) bool
- func IsMap(i interface{}) bool
- func IsNumber(i interface{}) bool
- func IsString(i interface{}) bool
- func ValidateStruct(target interface{}) (bool, error)
- func ValidateStructField(target interface{}, field string) (bool, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAnyFloat ¶ added in v0.19.0
func IsAnyFloat(i interface{}) bool
IsAnyFloat determines if i is a float32 or float64
func IsAnyInt ¶ added in v0.19.0
func IsAnyInt(i interface{}) bool
IsAnyInt determines if i is a int, int8, int16, in32 or int64
func IsArray ¶ added in v0.19.0
func IsArray(i interface{}) bool
IsArray determines if i is a slice or array
func IsFloat32 ¶ added in v0.19.0
func IsFloat32(i interface{}) bool
IsFloat32 determines if i is a float32
func IsFloat64 ¶ added in v0.19.0
func IsFloat64(i interface{}) bool
IsFloat64 determines if i is a float64
func IsNumber ¶ added in v0.19.0
func IsNumber(i interface{}) bool
IsNumber determines if i is a int or a float of any size
func IsString ¶ added in v0.19.0
func IsString(i interface{}) bool
IsString determines if i is a string
func ValidateStruct ¶
ValidateStruct validates all keys in a struct using their validate tag
func ValidateStructField ¶
ValidateStructField validates one field in a struct
Types ¶
This section is empty.