-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🐛 [Bug]: Validator using Fiber v3 or v2 is not using json tags for field names. #3299
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
Take a look at this code you wrote below, func NewValidatorError(err error) ValidatorError {
e := ValidatorError{}
e.Errors = make(map[string]interface{})
errs := err.(validator.ValidationErrors)
for _, v := range errs {
e.Errors[v.Field()] = fmt.Sprintf("%v", v.Tag())
}
return e
} like this photos, u can debug it in u local This issue has nothing to do with fiber, maybe you should go to validator to post an issue. my test code: package main
import (
"fmt"
"testing"
"github.com/go-playground/validator/v10"
)
// test main func
func Test_NewValidatorError(t *testing.T) {
userDTO := UserStatsCreateDTO{
FirstName: "John",
Age: 10,
BirthDate: "2024-01-01 12:00:00",
}
validate := validator.New()
err := validate.Struct(userDTO)
validationErrors := err.(validator.ValidationErrors)
fmt.Println(validationErrors[0].Field())
// fmt.Println(validationErrors[1].Field())
// fmt.Println(validationErrors[2].Field())
// fmt.Println(validationErrors[0].ActualTag())
// fmt.Println(validationErrors[1].ActualTag())
// fmt.Println(validationErrors[2].ActualTag())
fmt.Println(validationErrors[0].Tag())
// fmt.Println(validationErrors[1].Tag())
// fmt.Println(validationErrors[2].Tag())
} Couldn't using gin return different results than fiber? If so this discussion needs to be continued |
From the documentation: Field returns the fields name with the tag name taking precedence over the field's actual name. eq. JSON name "fname" see StructField for comparison And Tag() returns the tag that failed the validation. So, using Field() function should return the json tags from the struct properties. And this is not happening. |
@daibertdiego I also see this comment, but that's not what is actually shown in the test code. Do you think this has something to do with the fiber framework? Maybe I can try experimenting on the gin framework |
Yeah, it's related to Fiber framework. Using the validator out of the framework, shows the json tag for the Field() function. |
type UserStatsCreateDTO struct {
FirstName string `json:"first_name" validate:"required"`
Age int `json:"age" validate:"required,gt=5"`
BirthDate string `json:"birth_date" validate:"required,datetime=2006/01/02"`
}
func Test_NewValidatorError(t *testing.T) {
userDTO := UserStatsCreateDTO{
FirstName: "John",
Age: 10,
BirthDate: "2024-01-01 12:00:00",
}
validate := validator.New()
err := validate.Struct(userDTO)
validationErrors := err.(validator.ValidationErrors)
fmt.Println(validationErrors[0].Field())
require.Equal(t, "BirthDate", validationErrors[0].Field())
require.Equal(t, "datetime", validationErrors[0].Tag())
} https://pkg.go.dev/github.com/go-playground/validator/v10#FieldError i have tested it in a setup without fiber, should be an error in the validation package |
go-playground/validator#935 |
Bug Description
I'm using validator v10 with fiber v3 (but also tested on v2), and when I intercept validation errors, my custom messages are not using the json tag names, but the property names.
For example:
When I try to send an invalid request using this DTO, this is my response.
How to Reproduce
Steps to reproduce the behavior:
Expected Behavior
Receive the error msg using the json tag name like :
{
errors:[
"first_name": required,
"age": gt,
"birth_date": datetime
]
}
Fiber Version
v3
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: