From 42800b401e6c468edc1e9128235fcafc85183843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20Navarro?= Date: Thu, 6 Jun 2024 13:02:43 +0200 Subject: [PATCH] feat: custom error message for gte validation --- pkg/validator/error_messages.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/validator/error_messages.go b/pkg/validator/error_messages.go index 2d2c5e1..00a6bfa 100644 --- a/pkg/validator/error_messages.go +++ b/pkg/validator/error_messages.go @@ -15,6 +15,9 @@ var validationErrorMessages = map[string]string{ "validIssueTypeKeys": "Keys must be a valid issue type. Check the documentation for the list of valid issue types", "uniqueMapValues": "Values must be unique across all keys. Check the default values for possible collisions", } +var validationErroMessagesWithParam = map[string]string{ + "gte": "Must be greater than or equal to %s", +} func getPrettyErrors(validationErrors govalidator.ValidationErrors) string { var buffer bytes.Buffer @@ -23,8 +26,14 @@ func getPrettyErrors(validationErrors govalidator.ValidationErrors) string { errKey := fieldErr.Namespace() errMsg, ok := validationErrorMessages[fieldErr.Tag()] if !ok { - errMsg = fmt.Sprintf(fallbackErrMessage, fieldErr.Field(), fieldErr.Tag()) + errMsg, ok = validationErroMessagesWithParam[fieldErr.Tag()] + if !ok { + errMsg = fmt.Sprintf(fallbackErrMessage, fieldErr.Field(), fieldErr.Tag()) + } else { + errMsg = fmt.Sprintf(errMsg, fieldErr.Param()) + } } + buffer.WriteString(fmt.Sprintf("- %s: %s\n", errKey, errMsg)) }