Closed noonien closed 4 years ago
Fixed. Please let me know if you encounter any problem. Thanks!
@qiangxue I think the fix referenced does not work when the struct is declared within another struct, for example
type A struct {
Member1 int
Member2 string
SubStruct struct {
SubMember1 int
SubMember2 string
}
}
This gives error "field #2 cannot be found in the struct" .
I can still replicate this error.
@qiangxue I'm also experiencing this (on 3.5.0, go 1.11.5)
Any solution to using go gozzo when there's a struct in another struct?
any update on this? Im also encounter same problem.
type Some struct {
Order *SomeStruct `json:"order"`
}
type SomeStruct struct {
ID string `json:"id"`
}
func main() {
var some = new(Some)
str := `{"order":{"id":"123a"}}`
_ = json.Unmarshal([]byte(str), &some)
err := validation.ValidateStruct(some,
validation.Field(
&some.Order.ID,
validation.Match(regexp.MustCompile("^[a-zA-Z0-9]+$")).Error("must be valid order id"),
validation.Required, validation.Length(5, 50)),
)
log.Fatal(err)
// field #0 cannot be found in the struct
}
above gives error "field #0 cannot be found in the struct"
any updates on this? I'm also encountering the same problem
type CreateClubRequestModel struct {
Name string `json:"name"`
Username string `json:"username"`
Location Location `json:"location"`
Phone string `json:"phone"`
}
type
Location struct {
Long float64 `json:"long"`
Lat float64 `json:"lat"`
}
func (crm *CreateClubRequestModel) Validate() error{
return validation.ValidateStruct(
crm,
validation.Field(&crm.Name, validation.Required, validation.Length(1, 30)),
validation.Field(&crm.Username,
validation.Required,
is.LowerCase,
validation.Length(5, 30),
validation.Match(regexp.MustCompile("^[0-9a-z_.]+$")),
),
validation.Field(&crm.Location.Lat, validation.Required, is.Latitude),
validation.Field(&crm.Location.Long, validation.Required, is.Longitude),
validation.Field(&crm.Phone, is.E164),
)
}
Above gives field "#2 cannot be found in the struct"
Only anonymous struct fields can be declared in with the struct's ValidateStruct(). For non-anonymous embedded structs, you should declare their field validation rules in those structs' Validate().
Considering the following:
Calling
BookListingOptions{}.Validate()
returns the following error:field #2 cannot be found in the struct