Skip to content

Commit

Permalink
feat: provide a RegisterTagNameFunc() (#72)
Browse files Browse the repository at this point in the history
Provide a public RegisterTagNameFunc in `handler.go`. So
clients of Tonic, can register their own tag functions.

Fixes #71
  • Loading branch information
hmajid2301 authored Mar 5, 2021
1 parent 00ab55e commit ecd126f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*TODO*
.history/
15 changes: 15 additions & 0 deletions tonic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ func RegisterValidation(tagName string, validationFunc validator.Func) error {
return validatorObj.RegisterValidation(tagName, validationFunc)
}

// RegisterTagNameFunc registers a function to get alternate names for StructFields.
//
// eg. to use the names which have been specified for JSON representations of structs, rather than normal Go field names:
//
// validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
// name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
// if name == "-" {
// return ""
// }
// return name
// }
func RegisterTagNameFunc(registerTagFunc validator.TagNameFunc) {
validatorObj.RegisterTagNameFunc(registerTagFunc)
}

func initValidator() {
validatorOnce.Do(func() {
validatorObj = validator.New()
Expand Down

0 comments on commit ecd126f

Please sign in to comment.