Documentation ¶
Overview ¶
Package validation provides validations for struct fields based on a validation tag
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultMap = Map{}
DefaultMap is the default validation map used to tell if a struct is valid.
Functions ¶
func AddValidation ¶
AddValidation registers the validation specified by key to the known validations. If more than one validation registers with the same key, the last one will become the validation for that key using DefaultValidationMap.
Types ¶
type Interface ¶
type Interface interface { SetFieldIndex(index int) FieldIndex() int SetFieldName(name string) FieldName() string Validate(value interface{}, obj reflect.Value) *ValidationError }
Interface specifies the necessary methods a validation must implement to be compatible with this package
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an atomic validation map when two Set happen at the same time, latest that started wins.
func (*Map) AddValidation ¶
AddValidation registers the validation specified by key to the known validations. If more than one validation registers with the same key, the last one will become the validation for that key.
func (*Map) IsValid ¶
func (vm *Map) IsValid(object interface{}) (bool, []ValidationError)
IsValid determines if an object is valid based on its validation tags.
type Validation ¶
type Validation struct { Name string // contains filtered or unexported fields }
Validation is an implementation of a Interface and can be used to provide basic functionality to a new validation type through an anonymous field
func (*Validation) FieldIndex ¶
func (v *Validation) FieldIndex() int
FieldIndex retrieves the index of the field the validation was applied to
func (*Validation) FieldName ¶
func (v *Validation) FieldName() string
FieldName retrieves the name of the field the validation was applied to
func (*Validation) SetFieldIndex ¶
func (v *Validation) SetFieldIndex(index int)
SetFieldIndex stores the index of the field the validation was applied to
func (*Validation) SetFieldName ¶
func (v *Validation) SetFieldName(name string)
SetFieldName stores the name of the field the validation was applied to
func (*Validation) Validate ¶
func (v *Validation) Validate(value interface{}, obj reflect.Value) *ValidationError
Validate determines if the value is valid. Nil is returned if it is valid
type ValidationError ¶
func IsValid ¶
func IsValid(object interface{}) (bool, []ValidationError)
IsValid determines if an object is valid based on its validation tags using DefaultValidationMap.
Example (Format) ¶
type Person struct { // Email must be valid email Email string `validation:"format=email"` } var p Person ok, errs := IsValid(p) fmt.Println(ok, errs)
Output:
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type ValidationErrors ¶
type ValidationErrors []ValidationError
func (ValidationErrors) Error ¶
func (e ValidationErrors) Error() string