Skip to content

Commit

Permalink
feat: custom error message for gte validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hielfx committed Jun 6, 2024
1 parent 8aebb0a commit 42800b4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/validator/error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}

Expand Down

0 comments on commit 42800b4

Please sign in to comment.