package api import ( "fmt" "git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/i18nresource" "github.com/go-playground/validator/v10" ) type apiValidationError struct { Field string `json:"field"` Err string `json:"error"` } func getValidationErrors(ac appcontext.AppContext, ve *validator.ValidationErrors) []apiValidationError { errors := make([]apiValidationError, len(*ve)) for i, fe := range *ve { errors[i] = apiValidationError{ Field: fe.Field(), Err: computeValidationMessage(ac, &fe), } } return errors } func computeValidationMessage(ac appcontext.AppContext, fe *validator.FieldError) string { tag := (*fe).Tag() switch tag { case "required": return i18nresource.GetTranslatedMessage(ac, "ValidationRequired") case "min": return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooShort"), (*fe).Param()) case "max": return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooLong"), (*fe).Param()) default: return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationPropertyFail"), tag) } }