diff --git a/binding/bind.go b/binding/bind.go index ce3531a..a4b8a4a 100644 --- a/binding/bind.go +++ b/binding/bind.go @@ -72,7 +72,7 @@ func (b *Binding) Bind(structPointer interface{}, req *http.Request, pathParams return err } -// Validate validates whether the fields of v is valid. +// Validate validates whether the fields of value is valid. func (b *Binding) Validate(value interface{}) error { return b.vd.Validate(value) } diff --git a/binding/default.go b/binding/default.go new file mode 100644 index 0000000..c472152 --- /dev/null +++ b/binding/default.go @@ -0,0 +1,42 @@ +package binding + +import "net/http" + +var defaultBinding = New(nil) + +// Default returns the default binding. +// NOTE: +// path tag name is 'path'; +// query tag name is 'query'; +// header tag name is 'header'; +// cookie tag name is 'cookie'; +// rawbody tag name is 'rawbody'; +// form tag name is 'form'; +// validator tag name is 'vd'; +// protobuf tag name is 'protobuf'; +// json tag name is 'json'. +func Default() *Binding { + return defaultBinding +} + +// SetErrorFactory customizes the factory of validation error. +// NOTE: +// If errFactory==nil, the default is used +func SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) { + defaultBinding.SetErrorFactory(bindErrFactory, validatingErrFactory) +} + +// BindAndValidate binds the request parameters and validates them if needed. +func BindAndValidate(structPointer interface{}, req *http.Request, pathParams PathParams) error { + return defaultBinding.BindAndValidate(structPointer, req, pathParams) +} + +// Bind binds the request parameters. +func Bind(structPointer interface{}, req *http.Request, pathParams PathParams) error { + return defaultBinding.Bind(structPointer, req, pathParams) +} + +// Validate validates whether the fields of value is valid. +func Validate(value interface{}) error { + return defaultBinding.Validate(value) +}