-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing new validator package (from #959) to simplify code in the…
… following PRs
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
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
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,32 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
ut "github.com/go-playground/universal-translator" | ||
"github.com/go-playground/validator/v10" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
func wrapError(err error, trans ut.Translator) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return &validationError{err: err, t: trans} | ||
} | ||
|
||
type validationError struct { | ||
err error | ||
t ut.Translator | ||
} | ||
|
||
func (v *validationError) Error() string { | ||
var errs validator.ValidationErrors | ||
if errors.As(v.err, &errs) { | ||
return strings.Join(maps.Values(errs.Translate(v.t)), ", ") | ||
} | ||
|
||
return v.err.Error() | ||
} |
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,35 @@ | ||
package validation | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/go-playground/locales/en" | ||
ut "github.com/go-playground/universal-translator" | ||
"github.com/go-playground/validator/v10" | ||
entranslations "github.com/go-playground/validator/v10/translations/en" | ||
) | ||
|
||
var ( | ||
validate *validator.Validate //nolint:gochecknoglobals | ||
translate ut.Translator //nolint:gochecknoglobals | ||
) | ||
|
||
//nolint:gochecknoinits | ||
func init() { | ||
enLoc := en.New() | ||
uni := ut.New(enLoc, enLoc) | ||
translate, _ = uni.GetTranslator("en") | ||
validate = validator.New(validator.WithRequiredStructEnabled()) | ||
|
||
err := entranslations.RegisterDefaultTranslations(validate, translate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
validate.RegisterTagNameFunc(func(fld reflect.StructField) string { | ||
return "'" + strings.SplitN(fld.Tag.Get("mapstructure"), ",", 2)[0] + "'" // nolint: gomnd | ||
}) | ||
} | ||
|
||
func ValidateStruct(s any) error { return wrapError(validate.Struct(s), translate) } |