Documentation ¶
Index ¶
- Constants
- Variables
- func ContextWithFactory(ctx context.Context, c Factory) context.Context
- func ContextWithTagKey(ctx context.Context, namedTagKey string) context.Context
- func FloatLengthOfDigit(f float64) (uint, uint)
- func FloatRuleParam(r *Rule) (digits uint64, decimal *uint64, err error)
- func FloatRuleRange(r *Rule, digits uint, decimal *uint) (*float64, *float64, error)
- func FloatRuleValues(r *Rule, digits uint, decimal *uint) (multiple float64, enums map[float64]string, err error)
- func IntRuleBitSize(r *Rule) (bits uint64, err error)
- func IntRuleRange(r *Rule, bits uint) (*int64, *int64, error)
- func IntRuleValues(r *Rule, bits int) (multiple int64, enums map[int64]string, err error)
- func IsFloatMultipleOf(v float64, div float64, decimal uint) bool
- func MaxInt(bits uint) int64
- func MaxUint(bits uint) uint64
- func MinInt(bits uint) int64
- func NewFactory() *factory
- func ParseFloatValue(b []byte, digits uint, decimal *uint) (float64, error)
- func RangeFromUint(min uint64, max *uint64) []*rules.Lit
- func StringRuleValues(r *Rule) map[string]string
- func TagKeyFromContext(ctx context.Context) string
- func UintRuleBitSize(r *Rule) (bits uint64, err error)
- func UintRuleRange(r *Rule, typ string, bits uint) (uint64, *uint64, error)
- func UintRuleValues(r *Rule, bits int) (multiple uint64, enums map[uint64]string, err error)
- type CanValidate
- type Creator
- type Factory
- type Float
- type Int
- type Loader
- type Map
- type Modifier
- type PreprocessStage
- type Processor
- type Rule
- type Slice
- type StrFmt
- type StrLenMode
- type String
- type Struct
- type Uint
- type ValidateFunc
- type Validator
Examples ¶
Constants ¶
View Source
const ( TagValidate = "validate" TagDefault = "default" TagErrMsg = "errMsg" )
Variables ¶
View Source
var ( TargetFloatValue = "float value" TargetDecimalDigitsOfFloatValue = "decimal digits of float value" TargetTotalDigitsOfFloatValue = "total digits of float value" )
View Source
var ( TargetStringLength = "string length" TargetStringValue = "string value" )
View Source
var DefaultFactory = NewFactory()
View Source
var StrLenModes = map[StrLenMode]func(s string) uint64{ STR_LEN_MODE__LENGTH: func(s string) uint64 { return uint64(len(s)) }, STR_LEN_MODE__RUNE_COUNT: func(s string) uint64 { return uint64(utf8.RuneCount([]byte(s))) }, }
View Source
var (
TargetIntValue = "int value"
)
View Source
var (
TargetMapLength = "map length"
)
View Source
var TargetSliceLength = "slice length"
View Source
var TargetUintValue = "uint value"
Functions ¶
func ContextWithTagKey ¶
func FloatLengthOfDigit ¶
func FloatRuleRange ¶
func FloatRuleValues ¶
func IntRuleBitSize ¶
func IntRuleValues ¶
func NewFactory ¶
func NewFactory() *factory
func StringRuleValues ¶
func TagKeyFromContext ¶
func UintRuleBitSize ¶
Types ¶
type CanValidate ¶
type CanValidate interface {
Validate() error
}
type Factory ¶
type Factory interface {
Compile(context.Context, []byte, typesx.Type, ...Processor) (Validator, error)
}
func FactoryFromContext ¶
type Float ¶
type Float struct { MaxDigits uint DecimalDigits *uint Minimum *float64 Maximum *float64 ExclusiveMaximum bool ExclusiveMinimum bool MultipleOf float64 Enums map[float64]string }
func (*Float) SetDefault ¶
func (vf *Float) SetDefault()
type Int ¶
type Int struct { BitSize uint Minimum *int64 Maximum *int64 MultipleOf int64 ExclusiveMaximum bool ExclusiveMinimum bool Enums map[int64]string }
func (*Int) SetDefault ¶
func (vi *Int) SetDefault()
type Loader ¶
type Loader struct { Creator Creator Validator PreprocessStage DftValue []byte Optional bool ErrMsg []byte }
Loader load from creator
type Map ¶
type PreprocessStage ¶
type PreprocessStage int
const ( PreprocessSkip PreprocessStage = iota PreprocessString PreprocessPtr )
type Rule ¶
Rule with internal error message and type identifier
func (*Rule) SetDefaultValue ¶
func (*Rule) SetOptional ¶
type StrFmt ¶
type StrFmt struct {
// contains filtered or unexported fields
}
func NewRegexpStrfmtValidator ¶
Example ¶
fmt.Println(AlphaValidator.Validate("a")) fmt.Println(AlphaValidator.Validate("1")) fmt.Println(EmailValidator.Validate("a.b.c+123@xxx.com"))
Output: <nil> alpha ^[a-zA-Z]+$ not match 1 <nil>
func NewStrfmtValidator ¶
func NewStrfmtValidator(f ValidateFunc, name string, aliases ...string) *StrFmt
type StrLenMode ¶
type StrLenMode int
const ( STR_LEN_MODE__LENGTH StrLenMode = iota STR_LEN_MODE__RUNE_COUNT )
func ParseStrLenMode ¶
func ParseStrLenMode(s string) (StrLenMode, error)
func StringRuleParam ¶
func StringRuleParam(r *Rule) (mode StrLenMode, err error)
func (StrLenMode) String ¶
func (m StrLenMode) String() string
type String ¶
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
func NewStructValidator ¶
Example ¶
v := NewStructValidator("json") sv, err := v.New(ctx, &Rule{ Type: typesx.FromReflectType(reflect.TypeOf(&SomeStruct{}).Elem()), }) if err != nil { return } s := SomeStruct{ Slice: []string{"", ""}, SliceStruct: []SubStruct{{Int: 0}}, Map: map[string]string{"1": "", "11": "", "12": ""}, MapStruct: map[string]SubStruct{"222": SubStruct{}}, } err = sv.Validate(s) var ( errs = map[string]string{} keyPaths = make([]string, 0) flatten = err.(*errors.ErrorSet).Flatten() ) flatten.Each( func(ferr *errors.FieldError) { errs[ferr.Field.String()] = strconv.Quote(ferr.Error.Error()) keyPaths = append(keyPaths, ferr.Field.String()) }, ) sort.Strings(keyPaths) for i := range keyPaths { k := keyPaths[i] fmt.Println(k, errs[k]) }
Output: JustRequired "missing required field" Map.1 "missing required field" Map.1/key "string length should be larger than 2, but got invalid value 1" Map.11 "missing required field" Map.12 "missing required field" MapStruct.222.float "missing required field" MapStruct.222.int "missing required field" MapStruct.222.uint "missing required field" Named "missing required field" PtrFloat "missing required field" PtrInt "missing required field" PtrString "missing required field" PtrUint "missing required field" SliceStruct[0].float "missing required field" SliceStruct[0].int "missing required field" SliceStruct[0].uint "missing required field" Slice[0] "missing required field" Slice[1] "missing required field" SomeStringer "missing required field" String "missing required field" Struct.float "missing required field" Struct.int "missing required field" Struct.uint "missing required field" float "missing required field" int "missing required field" uint "missing required field"
type Uint ¶
type Uint struct { BitSize uint Minimum uint64 Maximum uint64 MultipleOf uint64 ExclusiveMaximum bool ExclusiveMinimum bool Enums map[uint64]string }
func (*Uint) SetDefault ¶
func (vu *Uint) SetDefault()
type ValidateFunc ¶
type ValidateFunc func(interface{}) error
Source Files ¶
- validator.go
- validator__float.go
- validator__int.go
- validator__map.go
- validator__slice.go
- validator__strfmt.go
- validator__string.go
- validator__struct.go
- validator__uint.go
- validator_helper_float.go
- validator_helper_int.go
- validator_helper_map.go
- validator_helper_slice.go
- validator_helper_string.go
- validator_helper_uint.go
Click to show internal directories.
Click to hide internal directories.