asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.04k stars 555 forks source link

Path uses field names while fields use json tag names #352

Open leighmcculloch opened 5 years ago

leighmcculloch commented 5 years ago

The errors generated by the package include a field name and a path to the field that has failed validating, but they are inconsistent with what the contents of those fields are.

The path always contains the Go struct field names, where-as the field will contain the json tag name if a json tag was defined on that field.

For this reason it's hard to use the path when also using json tags, since they aren't both in the same format.

Example

type Details struct {
    Email string `json:"email" valid:"email"`
}

type Person struct {
    Details   Details   `json:"details"`
}

If the email is set as "gogopher@example-com", the following error will be outputted:

(govalidator.Errors) (len=1) {
  (govalidator.Errors) (len=1) {
    (govalidator.Error) {
      Name: (string) (len=5) "email",
      Err: (*errors.errorString)({
        s: (string) (len=47) "gogopher@example-com does not validate as email"
      }),
      CustomErrorMessageExists: (bool) false,
      Validator: (string) (len=5) "email",
      Path: ([]string) (len=1) {
        (string) (len=7) "Details"
      }
    }
  }
}

https://play.golang.org/p/M3ZpBZwwNTy

The Path is Details which is the Go struct field name, not the json tag name, and the Name is email which is the json tag name.

bool64-llc commented 3 years ago

any updates ?

sergeyglazyrindev commented 3 years ago

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.