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: "foo@bar.me",
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?