From 0efc3629b009de6a5627268ff79b5d64eadb6b6f Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Wed, 18 Mar 2026 15:11:29 +0100 Subject: [PATCH] API: improve validation message greater/lower than Add translation and return a better error for lower than --- internal/i18nresource/locale.en.toml | 2 ++ internal/i18nresource/locale.fr.toml | 2 ++ internal/myvalidator/myvalidator.go | 6 ++++-- internal/routes/userbookputupdate.go | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/i18nresource/locale.en.toml b/internal/i18nresource/locale.en.toml index 8bb3513..93bbe01 100644 --- a/internal/i18nresource/locale.en.toml +++ b/internal/i18nresource/locale.en.toml @@ -3,6 +3,8 @@ AuthenticationSuccess = "Authentication was a success." ValidationRequired = "This field is required." ValidationTooShort = "This field is too short. It should be at least %s characters." ValidationTooLong = "This field is too long. It should be under %s characters." +ValidationLowerThan = "This field should be lower than %s." +ValidationGreaterThan = "This field should be greater than %s." ValidationPropertyFail = "Validation failed for '%s' property." RegistrationDisabled = "Registration has been disabled on this instance." UserAlreadyExists = "An user with this name already exists." diff --git a/internal/i18nresource/locale.fr.toml b/internal/i18nresource/locale.fr.toml index 35cb220..e280d80 100644 --- a/internal/i18nresource/locale.fr.toml +++ b/internal/i18nresource/locale.fr.toml @@ -3,6 +3,8 @@ AuthenticationSuccess = "Connexion réussie." ValidationRequired = "Ce champ est requis." ValidationTooShort = "Ce champ est trop court. Il devrait contenir au moins %s caractères." ValidationTooLong = "Ce champ est trop long. Il ne devrait pas dépasser %s caractères." +ValidationLowerThan = "Ce champ devrait être inférieur à %s." +ValidationGreaterThan = "Ce champ devrait être supérieur à %s." ValidationPropertyFail = "La validation a échoué pour la propriété '%s'." RegistrationDisabled = "La création de nouveaux comptes a été désactivée sur cette instance." UserAlreadyExists = "Un utilisateur avec le même nom existe déjà." diff --git a/internal/myvalidator/myvalidator.go b/internal/myvalidator/myvalidator.go index 552d01f..2b02ff0 100644 --- a/internal/myvalidator/myvalidator.go +++ b/internal/myvalidator/myvalidator.go @@ -79,10 +79,12 @@ func computeValidationMessage(ac *appcontext.AppContext, fe *validator.FieldErro return i18nresource.GetTranslatedMessage(ac, "ValidationRequired") case "min": return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooShort"), (*fe).Param()) - case "gte": - return fmt.Sprintf("Should be greater than %s", (*fe).Param()) case "max": return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooLong"), (*fe).Param()) + case "lte": + return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationLowerThan"), (*fe).Param()) + case "gte": + return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationGreaterThan"), (*fe).Param()) default: return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationPropertyFail"), tag) } diff --git a/internal/routes/userbookputupdate.go b/internal/routes/userbookputupdate.go index d2edceb..3556bda 100644 --- a/internal/routes/userbookputupdate.go +++ b/internal/routes/userbookputupdate.go @@ -68,6 +68,7 @@ func PutUserBookHandler(ac appcontext.AppContext) { err = validateRating(*userBookPut.Rating) if err != nil { myvalidator.ReturnErrorsAsJsonResponse(&ac, err) + return } updateRating(&userbook, &userBookPut) }