Skip to content

Commit

Permalink
move to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi committed Mar 6, 2024
1 parent d1d09ef commit 4ec4093
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 30 deletions.
30 changes: 0 additions & 30 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,6 @@ func LoadConfiguration[T any](cfg *T, opts ...ConfigOption[T]) error {
return validateConfiguration(cfg)
}

func validateConfiguration[T any](cfg *T) []ValidationError {
var errs []*model.ValidationError

err := validate.Struct(cfg)
if err != nil {
errSlice := &validator.ValidationErrors{}
errors.As(err, errSlice)
for _, err := range *errSlice {
var element model.ValidationError
field, _ := reflect.ValueOf(cfg).Type().FieldByName(err.Field())
element.FailedField = field.Tag.Get("json")
if element.FailedField == "" {
element.FailedField = field.Tag.Get("query")
}
element.Tag = err.Tag()
element.Value = err.Param()
element.Type = err.Kind().String()

if _, ok := translatorMessages[element.Tag]; ok {
element.Message = err.Translate(translator)
} else {
element.Message = fmt.Sprintf("Field validation for '%s' failed on the '%s' tag", element.FailedField, element.Tag)
}

errs = append(errs, &element)
}
}
return errs
}

func (c *ConfigLoader[T]) populateConfiguration(cfg *T) error {
configYamlDir := c.ConfigYamlDir
path, err := filepath.Abs(configYamlDir)
Expand Down
100 changes: 100 additions & 0 deletions config/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package config

import (
"fmt"
"reflect"
"sync"

"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
)

var (
once sync.Once
validate *validator.Validate
translatorMessages map[string](func() string)
translator ut.Translator
)

func init() {
once.Do(func() {
validate = validator.New()
// err := validate.RegisterValidation("valid_env", ValidateEnvironment)
// if err != nil {
// logrus.Fatal("Failed to register custom validation")
// }
// err = validate.RegisterValidation("valid_env_dest", ValidateEnvironmentCopyDestination)
// if err != nil {
// logrus.Fatal("Failed to register custom validation")
// }

en := en.New()
uni := ut.New(en, en)
translator, _ = uni.GetTranslator("en")

translatorMessages = map[string](func() string){
// "valid_env": func() string {
// envs := []string{}
// for env := range model.EnvironmentMapping {
// envs = append(envs, env)
// }
// return fmt.Sprintf("{0} must be one of %s", envs)
// },
// "valid_env_dest": func() string {
// return "Copying configs from source env to destination env as specified is not allowed"
// },
}

for tag, getMessage := range translatorMessages {
err := validate.RegisterTranslation(
tag,
translator,
func(ut ut.Translator) error {
return ut.Add(tag, getMessage(), true) // see universal-translator for details
},
// use a function that returns a function here so the tag can be memoized
func(violatedTag string) validator.TranslationFunc {
return func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T(violatedTag, fe.Field())
return t
}
}(tag),
)
if err != nil {
return fmt.Errorf("Failed to register custom validation error translator: %w", err)
}
}
})
}

func validateConfiguration[T any](cfg *T) []ValidationError {
var errs []ValidationError

err := validate.Struct(cfg)
if err != nil {
errSlice := &validator.ValidationErrors{}
errors.As(err, errSlice)
for _, err := range *errSlice {
var element ValidationError
field, _ := reflect.ValueOf(cfg).Type().FieldByName(err.Field())
element.FailedField = field.Tag.Get("json")
if element.FailedField == "" {
element.FailedField = field.Tag.Get("query")
}
element.Tag = err.Tag()
element.Value = err.Param()
element.Type = err.Kind().String()

if _, ok := translatorMessages[element.Tag]; ok {
element.Message = err.Translate(translator)
} else {
element.Message = fmt.Sprintf("Field validation for '%s' failed on the '%s' tag", element.FailedField, element.Tag)
}

errs = append(errs, &element)
}
}
return errs
}

0 comments on commit 4ec4093

Please sign in to comment.