Open xeoncross opened 4 years ago
I see that PR #305 already attempted to address this. As mentioned in that PR, only the top-level struct name should be hidden because the client does not need to know what it is called neither does the client provide it.
My current workaround is used manually removing the first entity name from all top-level fields:
func removeTopStruct(fields map[string]string) map[string]string {
res := map[string]string{}
for field, err := range fields {
res[field[strings.Index(field, ".")+1:]] = err
}
return res
}
trans, _ := uni.GetTranslator("en")
fields := removeTopStruct(errs.Translate(trans))
{
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", " ")
enc.Encode(fields)
}
{
"Email": "Email must be a valid email address",
"FirstName": "FirstName is a required field"
}
+1
Package version: 10
Using the current translation, the struct name is included with validation error messages
Result:
Is there a way to change this to only show field names? Here is an example.
The reason is because I'm reading from a http.Request and the client does not provide the struct name, that is known only internally. This example is pulled from https://github.com/go-playground/validator/blob/master/_examples/struct-level/