Documentation ¶
Overview ¶
Package check allows data validation of values in different types
Example ¶
package main import "fmt" type Person struct { Name string } func main() { p := &Person{ Name: "invalid*", } s := Struct{ "Name": Composite{ NonEmpty{}, Regex{`^[a-zA-Z0-9]+$`}, MinChar{10}, }, } e := s.Validate(*p) if e.HasErrors() { err, ok := e.GetErrorsByKey("Name") if !ok { panic("key 'Name' does not exists") } fmt.Println(err) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorMessages contains default error messages ErrorMessages = map[string]string{ "nonZero": "value cannot be empty", "before": "%v is not before %v", "after": "%v is not after %v", "lowerThan": "%v is not lower than %v", "greaterThan": "%v is not greater than %v", "minChar": "too short, minimum %v characters", "maxChar": "too long, minimum %v characters", "email": "'%v' is an invalid email address", "regex": "'%v' does not match '%v'", "uuid": "'%v' is an invalid uuid", } )
Functions ¶
This section is empty.
Types ¶
type Composite ¶
type Composite []Validator
Composite allows adding multiple validators to the same value
type Email ¶
type Email struct{}
Email is a constraint to do a simple validation for email addresses, it only check if the string contains "@" and that it is not in the first or last character of the string. Also if there is only one "@" and if there is a "." in the string after the "@".
type Error ¶
Error is the default validation error. The Params() method returns the params to be used in error messages
type GreaterThan ¶
type GreaterThan struct {
Constraint float64
}
GreaterThan validates that a number must be greater than its value
func (GreaterThan) Validate ¶
func (validator GreaterThan) Validate(v interface{}) Error
Validate check value against constraint
type LowerThan ¶
type LowerThan struct {
Constraint float64
}
LowerThan validates that a number must be lower than its value
type MaxChar ¶
type MaxChar struct {
Constraint int
}
MaxChar validates that a string must have a length maximum of its constraint
type MinChar ¶
type MinChar struct {
Constraint int
}
MinChar validates that a string must have a length minimum of its constraint
type NonEmpty ¶
type NonEmpty struct{}
NonEmpty check that the value is not a zeroed value depending on its type
type Regex ¶
type Regex struct {
Constraint string
}
Regex allow validation usig regular expressions
type Struct ¶
Struct allows validation of structs
func (Struct) Validate ¶
func (s Struct) Validate(v interface{}) StructError
Validate execute validation using the validators.
type StructError ¶
StructError is a map with validation errors
func (StructError) Error ¶
func (e StructError) Error() string
Error satisfy error interface. Will return a concatenated strings of errors separated by "\n"
func (StructError) GetErrorsByKey ¶
func (e StructError) GetErrorsByKey(key string) ([]Error, bool)
GetErrorsByKey return all errors for a specificed key
func (StructError) HasErrors ¶
func (e StructError) HasErrors() bool
HasErrors return true if the ErrorMap has errors
func (StructError) ToMessages ¶
func (e StructError) ToMessages() map[string]map[string]string
ToMessages convert StructError to a map of field and their validation key with proper error messages
type UUID ¶
type UUID struct{}
UUID verify a string in the UUID format xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError implements Error
func NewValidationError ¶
func NewValidationError(key string, params ...interface{}) *ValidationError
NewValidationError create and return a ValidationError
func (ValidationError) Error ¶
func (e ValidationError) Error() string
func (ValidationError) ErrorMap ¶
func (e ValidationError) ErrorMap() map[string][]interface{}
ErrorMap returns the error map
type Validator ¶
type Validator interface { // Validate check value against constraints Validate(v interface{}) Error }
Validator is an interface for constraint types with a method of validate()
Example ¶
username := "invalid*" validator := CustomStringContainValidator{"admin"} e := validator.Validate(username) fmt.Println(ErrorMessages[e.Error()])
Output: