Leftovers from PR #30 (#36)

This commit is contained in:
gadotroee 2021-05-09 11:26:28 +03:00 committed by GitHub
parent 506e36fbdc
commit 3f0ce35de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import (
"mizuserver/pkg/database" "mizuserver/pkg/database"
"mizuserver/pkg/models" "mizuserver/pkg/models"
"mizuserver/pkg/utils" "mizuserver/pkg/utils"
"mizuserver/pkg/validation"
) )
const ( const (
@ -30,13 +31,12 @@ var (
func GetEntries(c *fiber.Ctx) error { func GetEntries(c *fiber.Ctx) error {
entriesFilter := new(models.EntriesFilter) entriesFilter := &models.EntriesFilter{}
if err := c.QueryParser(entriesFilter); err != nil { if err := c.QueryParser(entriesFilter); err != nil {
return err return c.Status(fiber.StatusBadRequest).JSON(err)
} }
err := validation.Validate(entriesFilter)
err := utils.Validate(entriesFilter)
if err != nil { if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(err) return c.Status(fiber.StatusBadRequest).JSON(err)
} }

View File

@ -1,4 +1,4 @@
package utils package validation
import ( import (
"fmt" "fmt"
@ -14,25 +14,22 @@ func Validate(object interface{}) (errs []string){
return translateError(err, trans) 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 { if err == nil {
return nil return nil
} }
validatorErrs := err.(validator.ValidationErrors) validatorErrs := err.(validator.ValidationErrors)
for _, e := range validatorErrs { for _, e := range validatorErrs {
translatedErr := fmt.Errorf(e.Translate(trans)).Error() translatedErr := fmt.Errorf(e.Translate(*trans)).Error()
errs = append(errs, translatedErr) errs = append(errs, translatedErr)
} }
return errs return errs
} }
func getValidator() (*validator.Validate, ut.Translator) { func getValidator() (*validator.Validate, *ut.Translator) {
validate := validator.New() validate := validator.New()
english := en.New() english := en.New()
uni := ut.New(english, english) trans, _ := ut.New(english, english).GetTranslator("en")
trans, _ := uni.GetTranslator("en")
_ = et.RegisterDefaultTranslations(validate, trans) _ = et.RegisterDefaultTranslations(validate, trans)
return validate, trans return validate, &trans
} }