diff --git a/api/pkg/controllers/entries_controller.go b/api/pkg/controllers/entries_controller.go index 3f607f9a7..79a41819b 100644 --- a/api/pkg/controllers/entries_controller.go +++ b/api/pkg/controllers/entries_controller.go @@ -8,6 +8,7 @@ import ( "mizuserver/pkg/database" "mizuserver/pkg/models" "mizuserver/pkg/utils" + "mizuserver/pkg/validation" ) const ( @@ -30,13 +31,12 @@ var ( func GetEntries(c *fiber.Ctx) error { - entriesFilter := new(models.EntriesFilter) + entriesFilter := &models.EntriesFilter{} if err := c.QueryParser(entriesFilter); err != nil { - return err + return c.Status(fiber.StatusBadRequest).JSON(err) } - - err := utils.Validate(entriesFilter) + err := validation.Validate(entriesFilter) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(err) } diff --git a/api/pkg/utils/validateUtils.go b/api/pkg/validation/validation.go similarity index 68% rename from api/pkg/utils/validateUtils.go rename to api/pkg/validation/validation.go index 73cff906c..ab6426ee1 100644 --- a/api/pkg/utils/validateUtils.go +++ b/api/pkg/validation/validation.go @@ -1,4 +1,4 @@ -package utils +package validation import ( "fmt" @@ -14,25 +14,22 @@ func Validate(object interface{}) (errs []string){ return translateError(err, trans) } -func translateError(err error, trans ut.Translator) (errs []string) { +func translateError(err error, trans *ut.Translator) (errs []string) { if err == nil { return nil } validatorErrs := err.(validator.ValidationErrors) for _, e := range validatorErrs { - translatedErr := fmt.Errorf(e.Translate(trans)).Error() + translatedErr := fmt.Errorf(e.Translate(*trans)).Error() errs = append(errs, translatedErr) } return errs } -func getValidator() (*validator.Validate, ut.Translator) { - +func getValidator() (*validator.Validate, *ut.Translator) { validate := validator.New() - english := en.New() - uni := ut.New(english, english) - trans, _ := uni.GetTranslator("en") + trans, _ := ut.New(english, english).GetTranslator("en") _ = et.RegisterDefaultTranslations(validate, trans) - return validate, trans + return validate, &trans }