Validator fails to validate elements (structs) of a list when they have required fields.
I have the following structs:
type CustomerOption struct {
Name string valid:"required" json:"name"
Value string json:"value"
}
// CustomerOptions a set of customer options (name, value pairs)
// swagger:model CustomerOptions
type CustomerOptions struct {
CustomerOptions []CustomerOption valid:"required" json:"List"
}
Context("CustomerOptions.Validate()", func() {
//The following test passes:
It("should error if no options", func() {
custOpts := types.CustomerOptions{
CustomerOptions: nil,
}
err := custOpts.Validate()
Expect(err).To(MatchError(oshandlers.NewValidationError().Field("List", "required")))
})
//But this one fails complaining that 'List' is required which is wrong since the List (CustomerOptions is provided). It should have failed validating CustomerOption. It used to work correctly before (I had older version).
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.
Validator fails to validate elements (structs) of a list when they have required fields.
I have the following structs:
type CustomerOption struct { Name string
valid:"required" json:"name"
Value stringjson:"value"
}// CustomerOptions a set of customer options (name, value pairs) // swagger:model CustomerOptions type CustomerOptions struct { CustomerOptions []CustomerOption
valid:"required" json:"List"
}func (custOpt *CustomerOption) Validate() error { validation.AddCustomValidators()
}
func (custOpts *CustomerOptions) Validate() error { validation.AddCustomValidators()
}
Context("CustomerOptions.Validate()", func() { //The following test passes: It("should error if no options", func() { custOpts := types.CustomerOptions{ CustomerOptions: nil, } err := custOpts.Validate() Expect(err).To(MatchError(oshandlers.NewValidationError().Field("List", "required"))) })
//But this one fails complaining that 'List' is required which is wrong since the List (CustomerOptions is provided). It should have failed validating CustomerOption. It used to work correctly before (I had older version).
})