-
Notifications
You must be signed in to change notification settings - Fork 7
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
map[string]*valueError
is private so i cannot return it
#28
Comments
Hi @khanakia, Thank you so much for reporting the issue you're experiencing. I've been considering making Valgo currently exposes a public Here's an example with your code: func RegisterValidate() *valgo.Error {
val := valgo.Is(
valgo.String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20),
valgo.Number(17, "age").GreaterThan(18),
)
if !val.Valid() {
return val.Error().(*valgo.Error)
}
return nil
}
func main() {
err := RegisterValidate()
if err != nil {
fmt.Println("full_name", err.Errors()["full_name"].Messages())
fmt.Println("age", err.Errors()["age"].Messages())
}
} Please, let me know if this makes sense to you, and if you see this as Go-programmer-friendly code. On other hand, I'm concerned that the Looking forward to your feedback. |
Anyway, i made it work by following the exact workaround you proposed. I am using graphql and when just a So I had to create a wrapper just to make the errors meaning full on the front end. Maybe if possible somehow we could get the first error message returned instead of just a generic message. Thanks |
I find this confusing too, but I'm new to Go so that probably has a lot to do with it. I also have been trying to figure out how to avoid marshalling the errors to JSON. I use HTMX with a RESTful API, so JSON is not passed anywhere. Thanks for the great library. I think it's what I've been searching for. |
Here's my workaround. It seems a very convoluted way of getting the error messages without using JSON. type postInput struct {
GivenName string
Password string
}
// Validate the input values and return a boolean indicating whether
// the input is valid, as well as a map of error messages if the input is invalid
func inputValidator(input *postInput) (bool, map[string]string) {
val := v.Is(
v.String(input.GivenName, "given name").Not().Blank().OfLengthBetween(1, 50),
v.String(input.Password, "password").Not().Blank().MinLength(10),
)
if !val.Valid() {
messageMap := make(map[string]string)
valErrors := val.Error().(*valgo.Error).Errors()
for key, value := range valErrors {
messageMap[key] = value.Messages()[0]
}
return false, messageMap
}
return true, nil
}
// Used like this
ok, messageMap := inputValidator(<input>)
if !ok {
for field, message := range messageMap {
fmt.Printf("%v: %v\n", field, message)
} |
I have func and i want to return errors how do I do that as valueError is private I do not want to JSON marshal unnecessarily and then unmarshal again to a custom struct?
The text was updated successfully, but these errors were encountered: