Documentation ¶
Index ¶
- Variables
- func ArrayError(index int, err error) error
- func FieldError(key string, field reflect.StructField, err error) error
- func IsZero(v interface{}) bool
- func MustBeArray(data interface{}, fn func(s reflect.Value) error) error
- func MustBeInt64(data interface{}, fn func(i int64) error) error
- func MustBeNumber(data interface{}, fn func(a float64) error) error
- func MustBeRegex(data string, fn func(r *regexp.Regexp) error) error
- func MustBeString(data interface{}, fn func(s string) error) error
- func MustBeStruct(data interface{}, fn func(data reflect.Value) error) error
- func MustBeTime(data interface{}, fn func(t time.Time) error) error
- func SetIncludeErrPath(value bool)
- func SetNumberEpsilon(eps float64)
- type ArrayFn
- type ArrayValidate
- type BoolFunc
- type Conditional
- type ErrType
- type Error
- type Keyable
- type NumberValidate
- type Rule
- func After(after time.Time) Rule
- func Array(fns ...ArrayFn) Rule
- func Before(before time.Time) Rule
- func ChangeTime(fn func(t time.Time) time.Time, fns ...Rule) Rule
- func CustomNumber(fn func(n float64) error) Rule
- func DoMath(fn func(n float64) float64, fns ...Rule) Rule
- func EQ(n float64) Rule
- func Equal(i interface{}) Rule
- func GT(n float64) Rule
- func GTE(n float64) Rule
- func In(arr interface{}) Rule
- func IsKind(kind reflect.Kind) Rule
- func LT(n float64) Rule
- func LTE(n float64) Rule
- func Match(regex string) Rule
- func MaxLength(l int) Rule
- func MaxSize(l int) Rule
- func MinLength(l int) Rule
- func MinSize(l int) Rule
- func NEQ(n float64) Rule
- func NotEqual(i interface{}) Rule
- func Number(fns ...Rule) Rule
- func Optional(fns ...Rule) Rule
- func String(fns ...Rule) Rule
- func StringCustom(fn func(s string) error) Rule
- func Struct(fns ...StructFn) Rule
- type RuleFn
- type Rules
- type StringValidate
- type StructFn
- type StructValidator
- type SwitchCase
- func (s *SwitchCase) Case(value interface{}, rule ...Rule) *SwitchCase
- func (s *SwitchCase) CaseCustom(ok BoolFunc, rule ...Rule) *SwitchCase
- func (s *SwitchCase) CaseMany(value []interface{}, rule ...Rule) *SwitchCase
- func (s *SwitchCase) Default(rule ...Rule) *SwitchCase
- func (s *SwitchCase) Do(data interface{}) error
- type TimeValidator
- type Valid
- type Validatable
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ( ErrRequired = errors.New("value required") ErrMustIn = func(arr interface{}) error { return fmt.Errorf("value must be one of %v", arr) } ErrNotEqual = func(i interface{}) error { return fmt.Errorf("value must be equal to %v", i) } ErrEqual = func(i interface{}) error { return fmt.Errorf("value must not be %v", i) } ErrNotKind = func(k reflect.Kind) error { return fmt.Errorf("value kind must be %v", k.String()) } )
Common error
var ( ErrNotArray = errors.New("must be an array") ErrMinSize = func(l int) error { return fmt.Errorf("size must be equal or greater than %v", l) } ErrMaxSize = func(l int) error { return fmt.Errorf("size must be equal or smaller than %v", l) } )
Array error
var ( ErrNotNumber = errors.New("not a number") ErrNEQ = func(n float64) error { return fmt.Errorf("must not be equal to %v", n) } ErrEQ = func(n float64) error { return fmt.Errorf("must be equal to %v", n) } ErrLT = func(n float64) error { return fmt.Errorf("must be less than %v", n) } ErrGT = func(n float64) error { return fmt.Errorf("must be greater than %v", n) } ErrLTE = func(n float64) error { return fmt.Errorf("must be equal or less than %v", n) } ErrGTE = func(n float64) error { return fmt.Errorf("must be equal or greater than %v", n) } )
var ( ErrNotString = errors.New("must be a string") ErrMaxLength = func(l int) error { return fmt.Errorf("string length must not be greater than %v", l) } ErrMinLength = func(l int) error { return fmt.Errorf("string length must be greater than %v", l) } ErrRegexNotMatch = func(regex string) error { return fmt.Errorf("string not match regex %v", regex) } ErrNotURL = errors.New("must be an url") ErrNotEmail = errors.New("must be an email address") ErrNotUUID = errors.New("string is not a valid UUID") ErrNotJSONString = errors.New("must be a json string") )
var ( ErrTime = errors.New("not a valid date") ErrNotInt64 = errors.New("must be int64") ErrNoTimeSource = errors.New("time source not chosen") ErrBefore = func(t time.Time) error { return fmt.Errorf("time must before %v", t) } ErrAfter = func(t time.Time) error { return fmt.Errorf("time must after %v", t) } )
var (
ErrNotStruct = errors.New("must be an object")
)
var NumberEpsilon = 1e-6
NumberEpsilon we need this because sometime compare 2 float64 could be stupid
Functions ¶
func FieldError ¶
func FieldError(key string, field reflect.StructField, err error) error
FieldError error for Field in Struct validator
func MustBeArray ¶ added in v1.0.1
MustBeArray check if data is array, if not return ErrNotArray
func MustBeInt64 ¶ added in v1.0.5
MustBeInt64 check if data is int64, if not return ErrNotInt64
func MustBeNumber ¶ added in v1.0.5
MustBeNumber check if data is Number, if not return ErrNotNumber
func MustBeRegex ¶ added in v1.0.1
MustBeRegex check if data is regexp.Regexp, if not return err
func MustBeString ¶ added in v1.0.1
MustBeString check if data is String, if not return ErrNotString
func MustBeStruct ¶ added in v1.0.1
MustBeStruct check if data is a struct, if not return ErrNotStruct
func MustBeTime ¶ added in v1.0.5
MustBeTime check if data is time.Time, if not return ErrTime
func SetIncludeErrPath ¶ added in v1.0.13
func SetIncludeErrPath(value bool)
SetIncludeErrPath set true if you want the error message include the full path globally
func SetNumberEpsilon ¶ added in v1.0.7
func SetNumberEpsilon(eps float64)
Types ¶
type ArrayFn ¶
type ArrayFn func(v *ArrayValidate)
ArrayFn Array function
type ArrayValidate ¶
type ArrayValidate struct {
// contains filtered or unexported fields
}
ArrayValidate validator for array
func (ArrayValidate) Do ¶
func (a ArrayValidate) Do(data interface{}) error
type BoolFunc ¶ added in v1.0.11
type BoolFunc func() bool
BoolFunc function return boolean value, to make sure it's not panic
type Conditional ¶
type Conditional struct {
// contains filtered or unexported fields
}
Conditional condition validator
func If ¶
func If(ok ...BoolFunc) *Conditional
If create new conditional for array, if data is non-zero then return Then, else return Else
func (*Conditional) Do ¶
func (c *Conditional) Do(data interface{}) error
func (*Conditional) Else ¶
func (c *Conditional) Else(fns ...Rule) *Conditional
Else validator if condition is zero
func (*Conditional) Then ¶
func (c *Conditional) Then(fns ...Rule) *Conditional
Then validator if condition is non-zero
type Error ¶
type Error struct { Type ErrType Key string Name string Err error // contains filtered or unexported fields }
Error common error type
func (*Error) GetLastErrorWithKey ¶
GetLastErrorWithKey return the last Error has Key
func (*Error) GetRootError ¶
GetRootError return the root error
func (*Error) IncludePath ¶ added in v1.0.14
type NumberValidate ¶ added in v1.0.5
type NumberValidate struct {
// contains filtered or unexported fields
}
NumberValidate Number validator
func (*NumberValidate) Do ¶ added in v1.0.5
func (v *NumberValidate) Do(data interface{}) error
type Rule ¶
type Rule interface {
Do(data interface{}) error
}
Rule common interface
var Email Rule = RuleFn(func(data interface{}) error { return MustBeString(data, func(s string) error { if _, err := mail.ParseAddress(s); err != nil { return ErrNotEmail } return nil }) })
Email check if string is valid Email using mail.ParseAddress, if not return ErrNotEmail
var JSONString Rule = RuleFn(func(data interface{}) error { return MustBeString(data, func(s string) error { var js interface{} if err := json.Unmarshal([]byte(s), &js); err != nil { return ErrNotJSONString } return nil }) })
JSONString check if string is a valid json, if not return ErrNotJSONString
var Require Rule = RuleFn(func(v interface{}) error { if IsZero(v) { return ErrRequired } return nil })
Require check data is empty use IsZero method, if not return ErrRequired
var URL Rule = RuleFn(func(data interface{}) error { return MustBeString(data, func(s string) error { if _, err := url.ParseRequestURI(s); err != nil { return ErrNotURL } return nil }) })
URL check if string is valid URL using url.ParseRequestURI, if not return ErrNotURL
var UUID Rule = RuleFn(func(data interface{}) error { return MustBeString(data, func(s string) error { if _, err := uuid.Parse(s); err != nil { return ErrNotUUID } return nil }) })
UUID check if string is valid UUID using uuid.Parse, if not return ErrNotUUID
func After ¶ added in v1.0.5
After check if data is time instance after given time, if not return ErrAfter
func Before ¶ added in v1.0.5
Before check if data is time instance before given time, if not return ErrBefore
func ChangeTime ¶ added in v1.0.5
ChangeTime modify the data time instance
func CustomNumber ¶ added in v1.0.5
CustomNumber custom validator for number
func Equal ¶ added in v1.0.5
func Equal(i interface{}) Rule
Equal check data equal to interface, if not return ErrNotEqual
func In ¶ added in v1.0.5
func In(arr interface{}) Rule
In check data is an element in array, if not return ErrMustIn
func NotEqual ¶ added in v1.0.5
func NotEqual(i interface{}) Rule
NotEqual check data equal to interface, if equal return ErrNotEqual
func StringCustom ¶
StringCustom custom string validator
type StringValidate ¶
type StringValidate struct {
// contains filtered or unexported fields
}
StringValidate validator for String
func (StringValidate) Validate ¶
func (s StringValidate) Validate() error
type StructValidator ¶
type StructValidator struct {
// contains filtered or unexported fields
}
StructValidator validate for Struct
func (*StructValidator) Do ¶
func (v *StructValidator) Do(data interface{}) error
type SwitchCase ¶
type SwitchCase struct {
// contains filtered or unexported fields
}
SwitchCase switch case validator
func (*SwitchCase) Case ¶
func (s *SwitchCase) Case(value interface{}, rule ...Rule) *SwitchCase
Case validator if value match case
func (*SwitchCase) CaseCustom ¶ added in v1.0.11
func (s *SwitchCase) CaseCustom(ok BoolFunc, rule ...Rule) *SwitchCase
CaseCustom custom validate, has higher priority than Case
func (*SwitchCase) CaseMany ¶
func (s *SwitchCase) CaseMany(value []interface{}, rule ...Rule) *SwitchCase
CaseMany same as Case but take multiple case
func (*SwitchCase) Default ¶
func (s *SwitchCase) Default(rule ...Rule) *SwitchCase
Default if not match any case, return default
func (*SwitchCase) Do ¶
func (s *SwitchCase) Do(data interface{}) error
type TimeValidator ¶ added in v1.0.5
type TimeValidator struct {
// contains filtered or unexported fields
}
func Date ¶ added in v1.0.5
func Date(layout string, loc *time.Location, fns ...Rule) *TimeValidator
Date parse date from layout string and loc, default layout is time.RFC3339, default loc is time.Local
func (*TimeValidator) Do ¶ added in v1.0.5
func (t *TimeValidator) Do(data interface{}) error
type Valid ¶
type Valid struct {
// contains filtered or unexported fields
}
Valid main validator
func (*Valid) IncludePath ¶ added in v1.0.14
type Validatable ¶
type Validatable interface {
Validate() error
}