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 any) bool
- func IsAnyInt(i any) bool
- func IsArray(i any) bool
- func IsBool(i any) bool
- func IsFloat32(i any) bool
- func IsFloat64(i any) bool
- func IsInt(i any) bool
- func IsInt16(i any) bool
- func IsInt32(i any) bool
- func IsInt64(i any) bool
- func IsInt8(i any) bool
- func IsIntFloat64(i any) bool
- func IsMap(i any) bool
- func IsNumber(i any) bool
- func IsString(i any) bool
- func ValidateStruct(target any) (bool, error)
- func ValidateStructField(target any, field string) (bool, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAnyFloat ¶ added in v0.19.0
IsAnyFloat determines if i is a float32 or float64
func IsIntFloat64 ¶ added in v0.21.0
IsIntFloat64 checks if a float64 is a whole integer, important when comparing data from JSON Unmarshal that's always float64 if an interface
func ValidateStruct ¶
ValidateStruct validates all keys in a struct using their validate tag
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.