-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(binding): Add global default function
Change-Id: I7fd43d85cd50ebe7cbe7c2ba0f89b4261b761507
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |