validator

package
v1.6.5-rc8-deb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

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 ContextWithFactory

func ContextWithFactory(ctx context.Context, c Factory) context.Context

func ContextWithTagKey

func ContextWithTagKey(ctx context.Context, namedTagKey string) context.Context

func FloatLengthOfDigit

func FloatLengthOfDigit(f float64) (uint, uint)

func FloatRuleParam

func FloatRuleParam(r *Rule) (digits uint64, decimal *uint64, err error)

func FloatRuleRange

func FloatRuleRange(r *Rule, digits uint, decimal *uint) (*float64, *float64, error)

func FloatRuleValues

func FloatRuleValues(r *Rule, digits uint, decimal *uint) (multiple float64, enums map[float64]string, err error)

func IntRuleBitSize

func IntRuleBitSize(r *Rule) (bits uint64, err error)

func IntRuleRange

func IntRuleRange(r *Rule, bits uint) (*int64, *int64, error)

func IntRuleValues

func IntRuleValues(r *Rule, bits int) (multiple int64, enums map[int64]string, err error)

func IsFloatMultipleOf

func IsFloatMultipleOf(v float64, div float64, decimal uint) bool

func MaxInt

func MaxInt(bits uint) int64

func MaxUint

func MaxUint(bits uint) uint64

func MinInt

func MinInt(bits uint) int64

func NewFactory

func NewFactory() *factory

func ParseFloatValue

func ParseFloatValue(b []byte, digits uint, decimal *uint) (float64, error)

func RangeFromUint

func RangeFromUint(min uint64, max *uint64) []*rules.Lit

func StringRuleValues

func StringRuleValues(r *Rule) map[string]string

func TagKeyFromContext

func TagKeyFromContext(ctx context.Context) string

func UintRuleBitSize

func UintRuleBitSize(r *Rule) (bits uint64, err error)

func UintRuleRange

func UintRuleRange(r *Rule, typ string, bits uint) (uint64, *uint64, error)

func UintRuleValues

func UintRuleValues(r *Rule, bits int) (multiple uint64, enums map[uint64]string, err error)

Types

type CanValidate

type CanValidate interface {
	Validate() error
}

type Creator

type Creator interface {
	Names() []string
	New(context.Context, *Rule) (Validator, error)
}

Creator interface can new a validator

type Factory

type Factory interface {
	Compile(context.Context, []byte, typesx.Type, ...Processor) (Validator, error)
}

func FactoryFromContext

func FactoryFromContext(ctx context.Context) Factory

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) Names

func (Float) Names() []string

func (Float) New

func (Float) New(ctx context.Context, r *Rule) (Validator, error)

func (*Float) SetDefault

func (vf *Float) SetDefault()

func (*Float) String

func (vf *Float) String() string

func (*Float) TypeCheck

func (vf *Float) TypeCheck(r *Rule) error

func (*Float) Validate

func (vf *Float) Validate(v interface{}) error

type Int

type Int struct {
	BitSize uint

	Minimum          *int64
	Maximum          *int64
	MultipleOf       int64
	ExclusiveMaximum bool
	ExclusiveMinimum bool

	Enums map[int64]string
}

func (Int) Names

func (Int) Names() []string

func (Int) New

func (Int) New(ctx context.Context, r *Rule) (Validator, error)

func (*Int) SetDefault

func (vi *Int) SetDefault()

func (*Int) String

func (vi *Int) String() string

func (*Int) TypeCheck

func (vi *Int) TypeCheck(r *Rule) error

func (*Int) Validate

func (vi *Int) Validate(v interface{}) error

type Loader

type Loader struct {
	Creator Creator
	Validator
	PreprocessStage

	DftValue []byte
	Optional bool
	ErrMsg   []byte
}

Loader load from creator

func NewLoader

func NewLoader(c Creator) *Loader

func (*Loader) New

func (l *Loader) New(ctx context.Context, rule *Rule) (Validator, error)

func (*Loader) String

func (l *Loader) String() string

func (*Loader) Validate

func (l *Loader) Validate(v interface{}) error

type Map

type Map struct {
	MinProperties uint64
	MaxProperties *uint64

	KeyValidator  Validator
	ElemValidator Validator
}

func (Map) Names

func (Map) Names() []string

func (*Map) New

func (vm *Map) New(ctx context.Context, r *Rule) (Validator, error)

func (*Map) String

func (vm *Map) String() string

func (*Map) Validate

func (vm *Map) Validate(v interface{}) error

func (*Map) ValidateReflectValue

func (vm *Map) ValidateReflectValue(rv reflect.Value) error

type Modifier

type Modifier interface {
	SetOptional(opt bool)
	SetDefaultValue(dft []byte)
	SetErrMsg(err []byte)
}

Modifier can change rule's option

type PreprocessStage

type PreprocessStage int
const (
	PreprocessSkip PreprocessStage = iota
	PreprocessString
	PreprocessPtr
)

type Processor

type Processor = func(rule Modifier)

type Rule

type Rule struct {
	*rules.Rule
	ErrMsg []byte
	Type   typesx.Type
}

Rule with internal error message and type identifier

func MustParseRuleByType

func MustParseRuleByType(r []byte, t typesx.Type) *Rule

func MustParseRuleStringByType

func MustParseRuleStringByType(r string, t typesx.Type) *Rule

func ParseRuleByType

func ParseRuleByType(r []byte, t typesx.Type) (*Rule, error)

func ParseRuleStringByType

func ParseRuleStringByType(r string, t typesx.Type) (*Rule, error)

func (*Rule) SetDefaultValue

func (r *Rule) SetDefaultValue(dft []byte)

func (*Rule) SetErrMsg

func (r *Rule) SetErrMsg(msg []byte)

func (*Rule) SetOptional

func (r *Rule) SetOptional(opt bool)

func (*Rule) String

func (r *Rule) String() string

type Slice

type Slice struct {
	ElemValidator Validator

	MinItems uint64
	MaxItems *uint64
}

func (Slice) Names

func (Slice) Names() []string

func (Slice) New

func (Slice) New(ctx context.Context, r *Rule) (Validator, error)

func (*Slice) String

func (vs *Slice) String() string

func (*Slice) TypeCheck

func (vs *Slice) TypeCheck(r *Rule) error

func (*Slice) Validate

func (vs *Slice) Validate(v interface{}) error

func (*Slice) ValidateReflectValue

func (vs *Slice) ValidateReflectValue(rv reflect.Value) error

type StrFmt

type StrFmt struct {
	// contains filtered or unexported fields
}

func NewRegexpStrfmtValidator

func NewRegexpStrfmtValidator(expr string, name string, aliases ...string) *StrFmt
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

func (*StrFmt) Names

func (v *StrFmt) Names() []string

func (StrFmt) New

func (v StrFmt) New(_ context.Context, r *Rule) (Validator, error)

func (*StrFmt) String

func (v *StrFmt) String() string

func (*StrFmt) TypeCheck

func (v *StrFmt) TypeCheck(r *Rule) error

func (*StrFmt) Validate

func (v *StrFmt) Validate(val interface{}) error

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 String struct {
	Enums   map[string]string
	Pattern *regexp.Regexp
	LenMode StrLenMode

	MinLength uint64
	MaxLength *uint64
}

func (String) Names

func (String) Names() []string

func (String) New

func (String) New(ctx context.Context, r *Rule) (Validator, error)

func (*String) String

func (vs *String) String() string

func (*String) TypeCheck

func (vs *String) TypeCheck(r *Rule) error

func (*String) Validate

func (vs *String) Validate(v interface{}) error

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

func NewStructValidator

func NewStructValidator(tagKey string) *Struct
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"

func (Struct) Names

func (Struct) Names() []string

func (*Struct) New

func (vs *Struct) New(ctx context.Context, r *Rule) (Validator, error)

func (*Struct) String

func (vs *Struct) String() string

func (*Struct) TypeCheck

func (vs *Struct) TypeCheck(r *Rule) error

func (*Struct) Validate

func (vs *Struct) Validate(v interface{}) error

func (*Struct) ValidateReflectValue

func (vs *Struct) ValidateReflectValue(rv reflect.Value) error

type Uint

type Uint struct {
	BitSize uint

	Minimum          uint64
	Maximum          uint64
	MultipleOf       uint64
	ExclusiveMaximum bool
	ExclusiveMinimum bool

	Enums map[uint64]string
}

func (Uint) Names

func (Uint) Names() []string

func (Uint) New

func (Uint) New(ctx context.Context, r *Rule) (Validator, error)

func (*Uint) SetDefault

func (vu *Uint) SetDefault()

func (*Uint) String

func (vu *Uint) String() string

func (*Uint) TypeCheck

func (vu *Uint) TypeCheck(r *Rule) error

func (*Uint) Validate

func (vu *Uint) Validate(v interface{}) error

type ValidateFunc

type ValidateFunc func(interface{}) error

type Validator

type Validator interface {
	Validate(interface{}) error
	String() string
}

Validator can validate input value

func MapRuleParam

func MapRuleParam(ctx context.Context, r *Rule) (key, elem Validator, err error)

func SliceElementRule

func SliceElementRule(ctx context.Context, r *Rule) (Validator, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL