Reputation: 15752
I want to extend go-validator to return a better type:
type Error map[string][]error
// Will output the first error when stringified (e.g. for json response).
func (err Error) Error() string {
for k, errs := range err {
return fmt.Sprintf("%s value had %s", k, errs[0].Error())
}
return "no error"
}
func Validate(v interface{}) error {
if ok, errs := DefaultValidator.Validate(v); !ok {
return Error(errs)
}
return nil
}
As I am now writing tests for this I ran into the problem that it the typed map Error
seems to have lost it indexing capabilities:
err = Validate(Value{
Foo: "bar",
Email: "[email protected]",
Required: "1234",
})
c.Check(err, check.IsNil)
err, ok := Validate(Value{}).(Error)
c.Check(ok, check.Equals, true)
c.Check(err["Foo"], check.DeepEquals, []error{validator.ErrMin})
c.Check(err["Email"], check.DeepEquals, []error{validator.ErrInvalid})
c.Check(err["Required"], check.DeepEquals, []error{validator.ErrZeroValue})
Returns:
model/validation/validation_test.go:42: invalid operation: err["Foo"] (type error does not support indexing)
model/validation/validation_test.go:43: invalid operation: err["Email"] (type error does not support indexing)
model/validation/validation_test.go:44: invalid operation: err["Required"] (type error does not support indexing)
I also tried to cast to type map[string][]error
but got "impossible type assertion".
What is wrong with my approach? How can I get indexing back to work?
Upvotes: 3
Views: 6229
Reputation: 36199
It seems like your err variable is initialised with error
type. When you do
err, ok := Validate(Value{}).(Error)
you merely check if err is actually an Error
. If you change err there to say errs, it should work.
Playground example: http://play.golang.org/p/ljNroPzVbd
Upvotes: 2