From b585460e60ca8bbcc34f642b00f4accf534a2c6e Mon Sep 17 00:00:00 2001 From: Dao Anh Dung Date: Thu, 8 Jun 2023 22:02:39 +0700 Subject: [PATCH] Change registry --- README.md | 2 +- cmd/project.go | 6 +- main.go | 2 +- template/tmpl/cmd/migrate/main.go.tmpl | 4 +- template/tmpl/config/redis.go.tmpl | 1 - template/tmpl/internal/app/app.go.tmpl | 4 +- template/tmpl/internal/app/seed.go.tmpl | 12 ++-- .../repository/password_reset_repo.go.tmpl | 7 +++ .../auth/repository/repository.go.tmpl | 11 ---- .../modules/auth/repository/role_repo.go.tmpl | 7 +++ .../modules/auth/repository/user_repo.go.tmpl | 7 +++ .../modules/auth/usecase/usecase.go.tmpl | 14 ----- .../name/repository/domain_repo.go.tmpl | 7 +++ .../name/repository/repository.go.tmpl | 9 --- .../modules/name/usecase/domain_uc.go.tmpl | 2 +- .../modules/name/usecase/usecase.go.tmpl | 10 ---- .../tmpl/internal/registry/repository.go.tmpl | 6 +- .../tmpl/internal/registry/usecase.go.tmpl | 19 +++++-- .../internal/repository/domain_repo.go.tmpl | 7 +++ .../internal/repository/repository.go.tmpl | 7 --- .../tmpl/internal/usecase/domain_uc.go.tmpl | 2 +- .../tmpl/internal/usecase/usecase.go.tmpl | 10 ---- .../{postgres => database}/migrate.go.tmpl | 2 +- .../{postgres => database}/postgres.go.tmpl | 2 +- template/tmpl/pkg/validate/base.go.tmpl | 57 ++++++++++++++----- 25 files changed, 119 insertions(+), 98 deletions(-) rename template/tmpl/pkg/{postgres => database}/migrate.go.tmpl (97%) rename template/tmpl/pkg/{postgres => database}/postgres.go.tmpl (94%) diff --git a/README.md b/README.md index 9205abe..9e5db45 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ USAGE: go-base-gen [global options] command [command options] [arguments...] VERSION: - v1.0.9 + v1.0.10 COMMANDS: project Generate base code for go project use clean architecture diff --git a/cmd/project.go b/cmd/project.go index 58e9461..05c510e 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -35,7 +35,7 @@ var ( "pkg/cache", "pkg/errors", "pkg/logger", - "pkg/postgres", + "pkg/database", "pkg/redis", "pkg/utils", "pkg/validate", @@ -96,8 +96,8 @@ var ( "pkg/errors/code.go.tmpl", "pkg/errors/errors.go.tmpl", "pkg/logger/logger.go.tmpl", - "pkg/postgres/migrate.go.tmpl", - "pkg/postgres/postgres.go.tmpl", + "pkg/database/migrate.go.tmpl", + "pkg/database/postgres.go.tmpl", "pkg/redis/redis.go.tmpl", "pkg/utils/bcrypt.go.tmpl", "pkg/utils/md5.go.tmpl", diff --git a/main.go b/main.go index a437b62..8d7a104 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( ) var ( - version string = "v1.0.9" + version string = "v1.0.10" ) func main() { diff --git a/template/tmpl/cmd/migrate/main.go.tmpl b/template/tmpl/cmd/migrate/main.go.tmpl index 066db04..63eef55 100644 --- a/template/tmpl/cmd/migrate/main.go.tmpl +++ b/template/tmpl/cmd/migrate/main.go.tmpl @@ -9,8 +9,8 @@ import ( "time" "{{.Project}}/config" + "{{.Project}}/pkg/database" "{{.Project}}/pkg/logger" - "{{.Project}}/pkg/postgres" ) func main() { @@ -30,7 +30,7 @@ func main() { db := config.GetDBConfig() - if err := postgres.Migrate(db); err != nil { + if err := database.Migrate(db); err != nil { logger.Error().Fatal(err) } } diff --git a/template/tmpl/config/redis.go.tmpl b/template/tmpl/config/redis.go.tmpl index de2a256..cfdfc14 100644 --- a/template/tmpl/config/redis.go.tmpl +++ b/template/tmpl/config/redis.go.tmpl @@ -35,5 +35,4 @@ func GetRedisConfig() Redis { return redisConf } - {{end}} diff --git a/template/tmpl/internal/app/app.go.tmpl b/template/tmpl/internal/app/app.go.tmpl index 1b574d7..37772d4 100644 --- a/template/tmpl/internal/app/app.go.tmpl +++ b/template/tmpl/internal/app/app.go.tmpl @@ -16,9 +16,9 @@ import ( "{{.Project}}/internal/constants" "{{.Project}}/internal/registry" "{{.Project}}/pkg/cache" + "{{.Project}}/pkg/database" "{{.Project}}/pkg/errors" "{{.Project}}/pkg/logger" - "{{.Project}}/pkg/postgres" "{{.Project}}/pkg/redis" "github.com/labstack/echo/v4" @@ -31,7 +31,7 @@ func Run(conf config.AppConfig) error { var db *gorm.DB var err error for { - db, err = postgres.NewGormDB(dbConf) + db, err = database.NewGormDB(dbConf) if err != nil { logger.Info().Printf("Wait for starting db: %v", err) time.Sleep(constants.ConnectWaitDuration) diff --git a/template/tmpl/internal/app/seed.go.tmpl b/template/tmpl/internal/app/seed.go.tmpl index 7c6927e..a8e9f2d 100644 --- a/template/tmpl/internal/app/seed.go.tmpl +++ b/template/tmpl/internal/app/seed.go.tmpl @@ -11,8 +11,8 @@ import ( "{{.Project}}/config" "{{.Project}}/internal/domain" authModule "{{.Project}}/internal/modules/auth/repository" + "{{.Project}}/pkg/database" "{{.Project}}/pkg/errors" - "{{.Project}}/pkg/postgres" "github.com/spf13/viper" ) @@ -26,12 +26,16 @@ type seedData struct { // Seed is function that seed data func Seed(dbConfig config.Database) error { - db, err := postgres.NewGormDB(dbConfig) + db, err := database.NewGormDB(dbConfig) if err != nil { return errors.ErrInternalServerError.Wrap(err) } - authModule := authModule.NewRepository(db) + authMD := &authModule.Repository{ + RoleR: authModule.NewRoleRepository(db), + UserR: authModule.NewUserRepository(db), + PasswordR: authModule.NewPasswordResetRepository(db), + } viper.SetConfigFile(pathJSON) if err = viper.ReadInConfig(); err != nil { @@ -43,7 +47,7 @@ func Seed(dbConfig config.Database) error { return errors.ErrInternalServerError.Wrap(err) } - if err := data.seedAuth(authModule); err != nil { + if err := data.seedAuth(authMD); err != nil { return errors.Throw(err) } diff --git a/template/tmpl/internal/modules/auth/repository/password_reset_repo.go.tmpl b/template/tmpl/internal/modules/auth/repository/password_reset_repo.go.tmpl index bfaec5d..5157166 100644 --- a/template/tmpl/internal/modules/auth/repository/password_reset_repo.go.tmpl +++ b/template/tmpl/internal/modules/auth/repository/password_reset_repo.go.tmpl @@ -21,6 +21,13 @@ type PasswordResetRepository struct { *gorm.DB } +// NewPasswordResetRepository will implement of domain.PasswordResetRepository interface +func NewPasswordResetRepository(db *gorm.DB) *PasswordResetRepository { + return &PasswordResetRepository{ + DB: db, + } +} + // StoreOrUpdate will store or update password reset by email func (rp *PasswordResetRepository) StoreOrUpdate(ctx context.Context, email, token string) error { dao := &PasswordReset{ diff --git a/template/tmpl/internal/modules/auth/repository/repository.go.tmpl b/template/tmpl/internal/modules/auth/repository/repository.go.tmpl index 9eb764b..63fc4f3 100644 --- a/template/tmpl/internal/modules/auth/repository/repository.go.tmpl +++ b/template/tmpl/internal/modules/auth/repository/repository.go.tmpl @@ -7,8 +7,6 @@ Mit License (MIT) import ( "{{.Project}}/internal/domain" - - "gorm.io/gorm" ) // Repository for module auth @@ -17,13 +15,4 @@ type Repository struct { UserR domain.UserRepository PasswordR domain.PasswordResetRepository } - -// NewRepository will create new postgres object -func NewRepository(db *gorm.DB) *Repository { - return &Repository{ - RoleR: &RoleRepository{DB: db}, - UserR: &UserRepository{DB: db}, - PasswordR: &PasswordResetRepository{DB: db}, - } -} {{end}} diff --git a/template/tmpl/internal/modules/auth/repository/role_repo.go.tmpl b/template/tmpl/internal/modules/auth/repository/role_repo.go.tmpl index 59ad7f8..79aacb2 100644 --- a/template/tmpl/internal/modules/auth/repository/role_repo.go.tmpl +++ b/template/tmpl/internal/modules/auth/repository/role_repo.go.tmpl @@ -19,6 +19,13 @@ type RoleRepository struct { *gorm.DB } +// NewRoleRepository will implement of domain.RoleRepository interface +func NewRoleRepository(db *gorm.DB) *RoleRepository { + return &RoleRepository{ + DB: db, + } +} + // Fetch will fetch content from db func (rp *RoleRepository) Fetch(ctx context.Context) ([]domain.Role, error) { dao := []Role{} diff --git a/template/tmpl/internal/modules/auth/repository/user_repo.go.tmpl b/template/tmpl/internal/modules/auth/repository/user_repo.go.tmpl index 48af287..d5cfab9 100644 --- a/template/tmpl/internal/modules/auth/repository/user_repo.go.tmpl +++ b/template/tmpl/internal/modules/auth/repository/user_repo.go.tmpl @@ -19,6 +19,13 @@ type UserRepository struct { *gorm.DB } +// NewUserRepository will implement of domain.UserRepository interface +func NewUserRepository(db *gorm.DB) *UserRepository { + return &UserRepository{ + DB: db, + } +} + // Fetch will fetch content from db func (rp *UserRepository) Fetch(ctx context.Context) ([]domain.User, error) { dao := []User{} diff --git a/template/tmpl/internal/modules/auth/usecase/usecase.go.tmpl b/template/tmpl/internal/modules/auth/usecase/usecase.go.tmpl index 3cff208..7dbb6f8 100644 --- a/template/tmpl/internal/modules/auth/usecase/usecase.go.tmpl +++ b/template/tmpl/internal/modules/auth/usecase/usecase.go.tmpl @@ -7,7 +7,6 @@ Mit License (MIT) import ( "{{.Project}}/internal/domain" - "{{.Project}}/internal/modules/auth/repository" ) // Usecase for module auth @@ -16,17 +15,4 @@ type Usecase struct { UserUC domain.UserUsecase AuthUC domain.AuthUsecase } - -// NewUsecase implements from interface -func NewUsecase( - repo *repository.Repository, - jwtSvc domain.JWTService, - thrSvc domain.ThrottleService, -) *Usecase { - return &Usecase{ - RoleUC: NewRoleUsecase(repo.RoleR), - UserUC: NewUserUsecase(repo.UserR), - AuthUC: NewAuthUsecase(jwtSvc, thrSvc, repo.UserR, repo.PasswordR), - } -} {{end}} diff --git a/template/tmpl/internal/modules/name/repository/domain_repo.go.tmpl b/template/tmpl/internal/modules/name/repository/domain_repo.go.tmpl index e67903d..71b65a9 100644 --- a/template/tmpl/internal/modules/name/repository/domain_repo.go.tmpl +++ b/template/tmpl/internal/modules/name/repository/domain_repo.go.tmpl @@ -19,6 +19,13 @@ type {{.Domain | capitalize}}Repository struct { *gorm.DB } +// New{{.Domain | capitalize}}Repository will implement of domain.{{.Domain | capitalize}}Repository +func New{{.Domain | capitalize}}Repository(db *gorm.DB) *{{.Domain | capitalize}}Repository { + return &{{.Domain | capitalize}}Repository{ + DB: db, + } +} + // Fetch will fetch content from db func (rp *{{.Domain | capitalize}}Repository) Fetch(ctx context.Context) ([]domain.{{.Domain | capitalize}}, error) { dao := []{{.Domain | capitalize}}{} diff --git a/template/tmpl/internal/modules/name/repository/repository.go.tmpl b/template/tmpl/internal/modules/name/repository/repository.go.tmpl index 0379e78..2c7d59c 100644 --- a/template/tmpl/internal/modules/name/repository/repository.go.tmpl +++ b/template/tmpl/internal/modules/name/repository/repository.go.tmpl @@ -7,19 +7,10 @@ Mit License (MIT) import ( "{{.Project}}/internal/domain" - - "gorm.io/gorm" ) // Repository for module auth type Repository struct { {{.Domain | capitalize}}R domain.{{.Domain | capitalize}}Repository } - -// NewRepository will create new postgres object -func NewRepository(db *gorm.DB) *Repository { - return &Repository{ - {{.Domain | capitalize}}R: &{{.Domain | capitalize}}Repository{DB: db}, - } -} {{end}} diff --git a/template/tmpl/internal/modules/name/usecase/domain_uc.go.tmpl b/template/tmpl/internal/modules/name/usecase/domain_uc.go.tmpl index bd82041..74a67d7 100644 --- a/template/tmpl/internal/modules/name/usecase/domain_uc.go.tmpl +++ b/template/tmpl/internal/modules/name/usecase/domain_uc.go.tmpl @@ -17,7 +17,7 @@ type {{.Domain | capitalize}}Usecase struct { repo domain.{{.Domain | capitalize}}Repository } -// New{{.Domain | capitalize}}Usecase will create new an {{.Domain}}Usecase object representation of domain.{{.Domain | capitalize}}Usecase interface +// New{{.Domain | capitalize}}Usecase will implement of domain.{{.Domain | capitalize}}Usecase interface func New{{.Domain | capitalize}}Usecase(repo domain.{{.Domain | capitalize}}Repository) *{{.Domain | capitalize}}Usecase { return &{{.Domain | capitalize}}Usecase{ repo: repo, diff --git a/template/tmpl/internal/modules/name/usecase/usecase.go.tmpl b/template/tmpl/internal/modules/name/usecase/usecase.go.tmpl index 62358c4..7fe3fcc 100644 --- a/template/tmpl/internal/modules/name/usecase/usecase.go.tmpl +++ b/template/tmpl/internal/modules/name/usecase/usecase.go.tmpl @@ -7,20 +7,10 @@ Mit License (MIT) import ( "{{.Project}}/internal/domain" - "{{.Project}}/internal/modules/{{.Module}}/repository" ) // Usecase for module auth type Usecase struct { {{.Domain | capitalize}}UC domain.{{.Domain | capitalize}}Usecase } - -// NewUsecase implements from interface -func NewUsecase( - repo *repository.Repository, -) *Usecase { - return &Usecase{ - {{.Domain | capitalize}}UC: New{{.Domain | capitalize}}Usecase(repo.{{.Domain | capitalize}}R), - } -} {{end}} diff --git a/template/tmpl/internal/registry/repository.go.tmpl b/template/tmpl/internal/registry/repository.go.tmpl index 8a5deaf..5d373c6 100644 --- a/template/tmpl/internal/registry/repository.go.tmpl +++ b/template/tmpl/internal/registry/repository.go.tmpl @@ -19,7 +19,11 @@ type Repository struct { // NewRepository implements from interface for modules func NewRepository(db *gorm.DB) *Repository { return &Repository{ - AuthModule: authRepo.NewRepository(db), + AuthModule: &authRepo.Repository{ + RoleR: authRepo.NewRoleRepository(db), + UserR: authRepo.NewUserRepository(db), + PasswordR: authRepo.NewPasswordResetRepository(db), + }, } } {{end}} diff --git a/template/tmpl/internal/registry/usecase.go.tmpl b/template/tmpl/internal/registry/usecase.go.tmpl index cb4ccb3..78567f0 100644 --- a/template/tmpl/internal/registry/usecase.go.tmpl +++ b/template/tmpl/internal/registry/usecase.go.tmpl @@ -17,11 +17,20 @@ type Usecase struct { // NewUsecase implements from interface for modules func NewUsecase(repo *Repository, svc *Service) *Usecase { return &Usecase{ - AuthModule: authUC.NewUsecase( - repo.AuthModule, - svc.JWTSvc, - svc.ThrottleSvc, - ), + AuthModule: &authUC.Usecase{ + RoleUC: authUC.NewRoleUsecase( + repo.AuthModule.RoleR, + ), + UserUC: authUC.NewUserUsecase( + repo.AuthModule.UserR, + ), + AuthUC: authUC.NewAuthUsecase( + svc.JWTSvc, + svc.ThrottleSvc, + repo.AuthModule.UserR, + repo.AuthModule.PasswordR, + ), + }, } } {{end}} diff --git a/template/tmpl/internal/repository/domain_repo.go.tmpl b/template/tmpl/internal/repository/domain_repo.go.tmpl index 8b6d3d3..eb925cf 100644 --- a/template/tmpl/internal/repository/domain_repo.go.tmpl +++ b/template/tmpl/internal/repository/domain_repo.go.tmpl @@ -19,6 +19,13 @@ type {{.Domain | capitalize}}Repository struct { *gorm.DB } +// New{{.Domain | capitalize}}Repository will implement of domain.{{.Domain | capitalize}}Repository interface +func New{{.Domain | capitalize}}Repository(db *gorm.DB) *{{.Domain | capitalize}}Repository { + return &{{.Domain | capitalize}}Repository{ + DB: db, + } +} + // Fetch will fetch content from db func (rp *{{.Domain | capitalize}}Repository) Fetch(ctx context.Context) ([]domain.{{.Domain | capitalize}}, error) { dao := []{{.Domain | capitalize}}{} diff --git a/template/tmpl/internal/repository/repository.go.tmpl b/template/tmpl/internal/repository/repository.go.tmpl index 2188d69..22c0e8b 100644 --- a/template/tmpl/internal/repository/repository.go.tmpl +++ b/template/tmpl/internal/repository/repository.go.tmpl @@ -13,11 +13,4 @@ import ( type Repository struct { {{.Domain | capitalize}}R domain.{{.Domain | capitalize}}Repository } - -// NewRepository will create new postgres object -func NewRepository() *Repository { - return &Repository{ - {{.Domain | capitalize}}R: &{{.Domain | capitalize}}Repository{}, - } -} {{end}} diff --git a/template/tmpl/internal/usecase/domain_uc.go.tmpl b/template/tmpl/internal/usecase/domain_uc.go.tmpl index cf23de7..7e2800e 100644 --- a/template/tmpl/internal/usecase/domain_uc.go.tmpl +++ b/template/tmpl/internal/usecase/domain_uc.go.tmpl @@ -17,7 +17,7 @@ type {{.Domain | capitalize}}Usecase struct { repo domain.{{.Domain | capitalize}}Repository } -// New{{.Domain | capitalize}}Usecase will create new an {{.Domain}}Usecase object representation of domain.{{.Domain | capitalize}}Usecase interface +// New{{.Domain | capitalize}}Usecase will implement of domain.{{.Domain | capitalize}}Usecase interface func New{{.Domain | capitalize}}Usecase(repo domain.{{.Domain | capitalize}}Repository) *{{.Domain | capitalize}}Usecase { return &{{.Domain | capitalize}}Usecase{ repo: repo, diff --git a/template/tmpl/internal/usecase/usecase.go.tmpl b/template/tmpl/internal/usecase/usecase.go.tmpl index 7b66bbd..6f13605 100644 --- a/template/tmpl/internal/usecase/usecase.go.tmpl +++ b/template/tmpl/internal/usecase/usecase.go.tmpl @@ -7,20 +7,10 @@ Mit License (MIT) import ( "{{.Project}}/internal/domain" - "{{.Project}}/internal/repository" ) // Usecase for all usecase type Usecase struct { {{.Domain | capitalize}}UC domain.{{.Domain | capitalize}}Usecase } - -// NewUsecase implements from interface -func NewUsecase( - repo *repository.Repository, -) *Usecase { - return &Usecase{ - {{.Domain | capitalize}}UC: New{{.Domain | capitalize}}Usecase(repo.{{.Domain | capitalize}}R), - } -} {{end}} diff --git a/template/tmpl/pkg/postgres/migrate.go.tmpl b/template/tmpl/pkg/database/migrate.go.tmpl similarity index 97% rename from template/tmpl/pkg/postgres/migrate.go.tmpl rename to template/tmpl/pkg/database/migrate.go.tmpl index a7bb42b..ab565a7 100644 --- a/template/tmpl/pkg/postgres/migrate.go.tmpl +++ b/template/tmpl/pkg/database/migrate.go.tmpl @@ -3,7 +3,7 @@ Copyright (c) 2023 dung13890. All rights reserved. Mit License (MIT) */}} -{{define "pkg/postgres/migrate.go"}}package postgres +{{define "pkg/database/migrate.go"}}package database import ( "flag" diff --git a/template/tmpl/pkg/postgres/postgres.go.tmpl b/template/tmpl/pkg/database/postgres.go.tmpl similarity index 94% rename from template/tmpl/pkg/postgres/postgres.go.tmpl rename to template/tmpl/pkg/database/postgres.go.tmpl index fad8e2a..6cc3052 100644 --- a/template/tmpl/pkg/postgres/postgres.go.tmpl +++ b/template/tmpl/pkg/database/postgres.go.tmpl @@ -3,7 +3,7 @@ Copyright (c) 2023 dung13890. All rights reserved. Mit License (MIT) */}} -{{define "pkg/postgres/postgres.go"}}package postgres +{{define "pkg/database/postgres.go"}}package database import ( "fmt" diff --git a/template/tmpl/pkg/validate/base.go.tmpl b/template/tmpl/pkg/validate/base.go.tmpl index 8452760..97ef1db 100644 --- a/template/tmpl/pkg/validate/base.go.tmpl +++ b/template/tmpl/pkg/validate/base.go.tmpl @@ -10,11 +10,15 @@ import ( "strings" "github.com/go-playground/locales/en" - uniTranslator "github.com/go-playground/universal-translator" + ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" + et "github.com/go-playground/validator/v10/translations/en" ) -var errTranslateNotFound = errors.New("Translator not found.") +var ( + errTranslateNotFound = errors.New("translator not found") + translationOverride = map[string]string{} +) // CustomValidate is struct used to validate type CustomValidate struct { @@ -26,55 +30,82 @@ func NewValidate() *CustomValidate { return &CustomValidate{validate: validator.New()} } +// RegisterAlias is used to register list of alias +func (v *CustomValidate) RegisterAlias(list map[string]string) { + for alias, tags := range list { + v.validate.RegisterAlias(alias, tags) + } +} + +// RegisterTranslationOverride is used to register list of translation override +func (*CustomValidate) RegisterTranslationOverride(list map[string]string) { + for tag, translation := range list { + translationOverride[tag] = translation + } +} + // Validate is used to validate interface func (v *CustomValidate) Validate(u interface{}) error { translator := en.New() - uni := uniTranslator.New(translator, translator) + uni := ut.New(translator, translator) trans, found := uni.GetTranslator("en") if !found { return errTranslateNotFound } + _ = et.RegisterDefaultTranslations(v.validate, trans) - _ = v.validate.RegisterTranslation("required", trans, func(ut uniTranslator.Translator) error { + _ = v.validate.RegisterTranslation("required", trans, func(ut ut.Translator) error { return ut.Add("required", "{0} must have a value!", true) - }, func(ut uniTranslator.Translator, fe validator.FieldError) string { + }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("required", fe.Field()) return t }) - _ = v.validate.RegisterTranslation("email", trans, func(ut uniTranslator.Translator) error { + _ = v.validate.RegisterTranslation("email", trans, func(ut ut.Translator) error { return ut.Add("email", "{0} invalid email!", true) - }, func(ut uniTranslator.Translator, fe validator.FieldError) string { + }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("email", fe.Field()) return t }) - _ = v.validate.RegisterTranslation("number", trans, func(ut uniTranslator.Translator) error { + _ = v.validate.RegisterTranslation("number", trans, func(ut ut.Translator) error { return ut.Add("number", "{0} is not number!", true) - }, func(ut uniTranslator.Translator, fe validator.FieldError) string { + }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("number", fe.Field()) return t }) - _ = v.validate.RegisterTranslation("min", trans, func(ut uniTranslator.Translator) error { + _ = v.validate.RegisterTranslation("min", trans, func(ut ut.Translator) error { return ut.Add("min", "{0} is less than min!", true) - }, func(ut uniTranslator.Translator, fe validator.FieldError) string { + }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("min", fe.Field()) return t }) - _ = v.validate.RegisterTranslation("max", trans, func(ut uniTranslator.Translator) error { + _ = v.validate.RegisterTranslation("max", trans, func(ut ut.Translator) error { return ut.Add("max", "{0} is greater than max!", true) - }, func(ut uniTranslator.Translator, fe validator.FieldError) string { + }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("max", fe.Field()) return t }) + if len(translationOverride) > 0 { + for tag, translation := range translationOverride { + _ = v.validate.RegisterTranslation(tag, trans, func(ut ut.Translator) error { + return ut.Add(tag, translation, true) + }, func(ut ut.Translator, fe validator.FieldError) string { + t, _ := ut.T(tag, fe.Field(), fe.Param()) + + return t + }) + } + } + if err := v.validate.Struct(u); err != nil { errArr := []string{} validationErrs := validator.ValidationErrors{}