From ecd126f80b95fae7372be52e0e0f119d4e46f5d2 Mon Sep 17 00:00:00 2001 From: Haseeb Majid Date: Fri, 5 Mar 2021 15:18:56 +0000 Subject: [PATCH] feat: provide a RegisterTagNameFunc() (#72) Provide a public RegisterTagNameFunc in `handler.go`. So clients of Tonic, can register their own tag functions. Fixes #71 --- .gitignore | 1 + tonic/handler.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index 7ea2a93..f827b31 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *TODO* +.history/ \ No newline at end of file diff --git a/tonic/handler.go b/tonic/handler.go index 54be1d5..bcb99e8 100644 --- a/tonic/handler.go +++ b/tonic/handler.go @@ -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()