Documentation
¶
Index ¶
- func Bind(req *protocol.Request, obj interface{}, pathParams param.Params) error
- func BindAndValidate(req *protocol.Request, obj interface{}, pathParams param.Params) error
- func Validate(obj interface{}) error
- type BindConfig
- func (config *BindConfig) MustRegTypeUnmarshal(t reflect.Type, ...)
- func (config *BindConfig) RegTypeUnmarshal(t reflect.Type, fn inDecoder.CustomizeDecodeFunc) error
- func (config *BindConfig) UseStdJSONUnmarshaler()
- func (config *BindConfig) UseThirdPartyJSONUnmarshaler(fn func(data []byte, v interface{}) error)
- type Binder
- type StructValidatordeprecated
- type ValidateConfigdeprecated
- func NewValidateConfig() *ValidateConfigdeprecated
- type ValidateErrFactory
- type ValidatorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindAndValidate ¶
BindAndValidate binds data from *protocol.Request to obj and validates them if needed. NOTE:
obj should be a pointer.
Types ¶
type BindConfig ¶ added in v0.7.0
type BindConfig struct {
// LooseZeroMode if set to true,
// the empty string request parameter is bound to the zero value of parameter.
// NOTE:
// The default is false.
// Suitable for these parameter types: query/header/cookie/form .
LooseZeroMode bool
// DisableDefaultTag is used to add default tags to a field when it has no tag
// If is false, the field with no tag will be added default tags, for more automated binding. But there may be additional overhead.
// NOTE:
// The default is false.
DisableDefaultTag bool
// DisableStructFieldResolve is used to generate a separate decoder for a struct.
// If is false, the 'struct' field will get a single inDecoder.structTypeFieldTextDecoder, and use json.Unmarshal for decode it.
// It usually used to add json string to query parameter.
// NOTE:
// The default is false.
DisableStructFieldResolve bool
// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
// interface{} as a Number instead of as a float64.
// NOTE:
// The default is false.
// It is used for BindJSON().
EnableDecoderUseNumber bool
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
// return an error when the destination is a struct and the input contains object
// keys which do not match any non-ignored, exported fields in the destination.
// NOTE:
// The default is false.
// It is used for BindJSON().
EnableDecoderDisallowUnknownFields bool
// TypeUnmarshalFuncs registers customized type unmarshaler.
// NOTE:
// time.Time is registered by default
TypeUnmarshalFuncs map[reflect.Type]inDecoder.CustomizeDecodeFunc
// Validator is used to validate for BindAndValidate()
//
// Deprecated: use ValidatorFunc instead. You can create a ValidatorFunc
// from a StructValidator using MakeValidatorFunc()
Validator StructValidator
// ValidatorFunc is used to validate structs with custom validation logic.
// It replaces the deprecated Validator field and provides request context.
// NOTE:
// The default is nil. If set, this takes precedence over the Validator field.
// The function signature allows access to the request for context-aware validation.
ValidatorFunc func(req *protocol.Request, v any) error
}
BindConfig contains options for default bind behavior.
func NewBindConfig ¶ added in v0.7.0
func NewBindConfig() *BindConfig
func (*BindConfig) MustRegTypeUnmarshal ¶ added in v0.7.0
func (config *BindConfig) MustRegTypeUnmarshal(t reflect.Type, fn func(req *protocol.Request, params param.Params, text string) (reflect.Value, error))
MustRegTypeUnmarshal registers customized type unmarshaler. It will panic if exist error.
func (*BindConfig) RegTypeUnmarshal ¶ added in v0.7.0
func (config *BindConfig) RegTypeUnmarshal(t reflect.Type, fn inDecoder.CustomizeDecodeFunc) error
RegTypeUnmarshal registers customized type unmarshaler.
func (*BindConfig) UseStdJSONUnmarshaler ¶ added in v0.7.0
func (config *BindConfig) UseStdJSONUnmarshaler()
UseStdJSONUnmarshaler uses encoding/json as json library NOTE:
The current version uses encoding/json by default. UseStdJSONUnmarshaler will remain in effect once it has been called.
func (*BindConfig) UseThirdPartyJSONUnmarshaler ¶ added in v0.7.0
func (config *BindConfig) UseThirdPartyJSONUnmarshaler(fn func(data []byte, v interface{}) error)
UseThirdPartyJSONUnmarshaler uses third-party json library for binding NOTE:
UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.
type Binder ¶ added in v0.7.0
type Binder interface {
Name() string
Bind(*protocol.Request, interface{}, param.Params) error
BindQuery(*protocol.Request, interface{}) error
BindHeader(*protocol.Request, interface{}) error
BindPath(*protocol.Request, interface{}, param.Params) error
BindForm(*protocol.Request, interface{}) error
BindJSON(*protocol.Request, interface{}) error
BindProtobuf(*protocol.Request, interface{}) error
Validate(*protocol.Request, interface{}) error
}
func DefaultBinder ¶ added in v0.7.0
func DefaultBinder() Binder
func NewDefaultBinder ¶ added in v0.7.0
func NewDefaultBinder(config *BindConfig) Binder
type StructValidator
deprecated
added in
v0.7.0
type StructValidator interface {
ValidateStruct(interface{}) error
Engine() interface{}
ValidateTag() string
}
StructValidator defines the interface for struct validation.
Deprecated: Use ValidatorFunc in BindConfig instead. You can create a ValidatorFunc from a StructValidator using MakeValidatorFunc().
func DefaultValidator
deprecated
added in
v0.7.0
func DefaultValidator() StructValidator
DefaultValidator returns the default StructValidator instance that uses tagexpr validation. The validator uses the "vd" tag for validation expressions and provides comprehensive struct field validation capabilities.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead. For migration: convert this StructValidator to a ValidatorFunc using MakeValidatorFunc().
Example migration:
// Old way (deprecated) validator := binding.DefaultValidator() // New way (recommended) validatorFunc := binding.MakeValidatorFunc(binding.DefaultValidator()) server.WithCustomValidatorFunc(validatorFunc)
func NewValidator
deprecated
added in
v0.7.0
func NewValidator(config *ValidateConfig) StructValidator
NewValidator creates a new StructValidator with the given configuration.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead. You can convert the returned StructValidator to a ValidatorFunc using MakeValidatorFunc().
type ValidateConfig
deprecated
added in
v0.7.0
type ValidateConfig struct {
ValidateTag string
ErrFactory ValidateErrFactory
}
ValidateConfig configures validation behavior for the built-in StructValidator.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead.
func NewValidateConfig
deprecated
added in
v0.7.0
func NewValidateConfig() *ValidateConfig
NewValidateConfig creates a new ValidateConfig.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead.
func (*ValidateConfig) MustRegValidateFunc
deprecated
added in
v0.7.0
func (config *ValidateConfig) MustRegValidateFunc(funcName string, fn func(args ...interface{}) error, force ...bool)
MustRegValidateFunc registers validator function expression. NOTE:
If force=true, allow to cover the existed same funcName. MustRegValidateFunc will remain in effect once it has been called.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead.
func (*ValidateConfig) SetValidatorErrorFactory
deprecated
added in
v0.7.0
func (config *ValidateConfig) SetValidatorErrorFactory(errFactory ValidateErrFactory)
SetValidatorErrorFactory customizes the factory of validation error.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead.
func (*ValidateConfig) SetValidatorTag
deprecated
added in
v0.7.0
func (config *ValidateConfig) SetValidatorTag(tag string)
SetValidatorTag customizes the validation tag.
Deprecated: Use WithCustomValidatorFunc with a custom validation function instead.
type ValidateErrFactory ¶ added in v0.7.0
type ValidatorFunc ¶ added in v0.10.3
ValidatorFunc defines a validation function that can access request context. It takes a request and the object to validate, returning an error if validation fails.
func MakeValidatorFunc ¶ added in v0.10.3
func MakeValidatorFunc(s StructValidator) ValidatorFunc
MakeValidatorFunc creates a validation function from a StructValidator. It optimizes validation by caching tag analysis results and skipping validation entirely for types that don't have validation tags.