From 5858f81d0a70303aa2aa4cce236656ab7708d778 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Tue, 13 Jan 2026 07:55:40 +0700 Subject: [PATCH] [validator] add JSON tags support --- validator.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/validator.go b/validator.go index a1cbb46..75d7e21 100644 --- a/validator.go +++ b/validator.go @@ -1,6 +1,11 @@ package validatorfx -import "github.com/go-playground/validator/v10" +import ( + "reflect" + "strings" + + "github.com/go-playground/validator/v10" +) type Validator = validator.Validate @@ -8,5 +13,15 @@ type Validator = validator.Validate // WithRequiredStructEnabled ensures the "required" family of tags (e.g., required, required_if) // apply to non-pointer struct fields, treating their zero values as invalid. func New() *Validator { - return validator.New(validator.WithRequiredStructEnabled()) + v := validator.New(validator.WithRequiredStructEnabled()) + v.RegisterTagNameFunc(func(fld reflect.StructField) string { + //nolint:mnd //fixed length + name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] + if name == "-" { + return "" + } + return name + }) + + return v }