Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages: add original value that caused an error #184

Open
hcaloto opened this issue Nov 29, 2022 · 0 comments
Open

Improve error messages: add original value that caused an error #184

hcaloto opened this issue Nov 29, 2022 · 0 comments

Comments

@hcaloto
Copy link

hcaloto commented Nov 29, 2022

System (please complete the following information):

  • OS: linux
  • GO Version: 1.19
  • Pkg Version: v1.4.5

Improvement:

It would be useful to have the original value in the final error message. In my use case I'm comparing a lot of rules at the same time and, sometimes, is not easy to see the original value that caused the error.

Example:

This is an approach to tackle it, for sure there are other better ways to do it, for example by using an error template.

--- validating.go       2022-11-29 10:38:22.535892280 +0100
+++ vendor/github.com/gookit/validate/validating.go     2022-11-29 10:38:31.228000046 +0100
@@ -1,7 +1,6 @@
 package validate
 
 import (
-       "fmt"
        "reflect"
        "strings"
 )
@@ -162,7 +161,7 @@
                if r.valueValidate(field, name, val, v) {
                        v.safeData[field] = val // save validated value.
                } else { // build and collect error message
-                       v.AddError(field, r.validator, fmt.Sprintf("%s, value:%v", r.errorMessage(field, r.validator, v), val))
+                       v.AddError(field, r.validator, r.errorMessage(field, r.validator, v))
                }
 
                // stop on error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant