Issue
I had issue when I validate entity like this,
type UserProfile struct {
NestedEntity *NestedEntity `json:"nested_entity" validate:"required,omitempty"`
}
type NestedEntity struct {
SubNestedEntity *SubNestedEntity `json:"sub_nested_entity" validate:"required,omitempty"`
}
type SubNestedEntity struct {
TypeData string `json:"type_data" validate:"required"`
}
return of validate is like this,
{
"": "The field is required"
}
Solution
In this file (github.com/fazpass/goliath/v3/helper/validator)we need add this line of codes to identify the name of attribute
30. if field == "" {
31. field = e.Field()
32. }
This is full file
package validator
import (
"fmt"
"reflect"
govalidator "github.com/go-playground/validator/v10"
)
func Validate(s interface{}) map[string]string {
var (
structValue = reflect.ValueOf(s)
structValueIndirect = reflect.Indirect(structValue)
structType = structValueIndirect.Type()
validator = govalidator.New()
err = validator.Struct(s)
errs = map[string]string{}
)
if err == nil {
return nil
}
validatorErrs := err.(govalidator.ValidationErrors)
for _, e := range validatorErrs {
var (
structfField, _ = structType.FieldByName(e.Field())
field = structfField.Tag.Get("json")
)
if field == "" {
field = e.Field()
}
errs[field] = GetMessage(field, e.ActualTag())
}
return errs
}
Issue
I had issue when I validate entity like this,
return of validate is like this,
{ "": "The field is required" }Solution
In this file (
github.com/fazpass/goliath/v3/helper/validator)we need add this line of codes to identify the name of attributeThis is full file