github.com/bytedance/go-tagexpr@v2.7.5-0.20210114074101-de5b8743ad85+incompatible/binding/default.go (about) 1 package binding 2 3 import "net/http" 4 5 var defaultBinding = New(nil) 6 7 // Default returns the default binding. 8 // NOTE: 9 // path tag name is 'path'; 10 // query tag name is 'query'; 11 // header tag name is 'header'; 12 // cookie tag name is 'cookie'; 13 // raw_body tag name is 'raw_body'; 14 // form tag name is 'form'; 15 // validator tag name is 'vd'; 16 // protobuf tag name is 'protobuf'; 17 // json tag name is 'json'; 18 // LooseZeroMode is false. 19 func Default() *Binding { 20 return defaultBinding 21 } 22 23 // SetLooseZeroMode if set to true, 24 // the empty string request parameter is bound to the zero value of parameter. 25 // NOTE: 26 // The default is false; 27 // Suitable for these parameter types: query/header/cookie/form . 28 func SetLooseZeroMode(enable bool) { 29 defaultBinding.SetLooseZeroMode(enable) 30 } 31 32 // SetErrorFactory customizes the factory of validation error. 33 // NOTE: 34 // If errFactory==nil, the default is used 35 func SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) { 36 defaultBinding.SetErrorFactory(bindErrFactory, validatingErrFactory) 37 } 38 39 // BindAndValidate binds the request parameters and validates them if needed. 40 func BindAndValidate(structPointer interface{}, req *http.Request, pathParams PathParams) error { 41 return defaultBinding.BindAndValidate(structPointer, req, pathParams) 42 } 43 44 // Bind binds the request parameters. 45 func Bind(structPointer interface{}, req *http.Request, pathParams PathParams) error { 46 return defaultBinding.Bind(structPointer, req, pathParams) 47 } 48 49 // Validate validates whether the fields of value is valid. 50 func Validate(value interface{}) error { 51 return defaultBinding.Validate(value) 52 }