Skip to content

Commit

Permalink
feat(binding): Add global default function
Browse files Browse the repository at this point in the history
Change-Id: I7fd43d85cd50ebe7cbe7c2ba0f89b4261b761507
  • Loading branch information
andeya committed Jun 11, 2019
1 parent bef0beb commit 4d2ab44
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion binding/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
42 changes: 42 additions & 0 deletions binding/default.go
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)
}

0 comments on commit 4d2ab44

Please sign in to comment.