add validator wrapper (#370)

all requests that have method Validate() error can be used to validate incoming request

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2019-07-07 13:25:20 +03:00
commit 540e370613

25
validator.go Normal file
View File

@ -0,0 +1,25 @@
package validator
import (
"context"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/server"
)
type Validator interface {
Validate() error
}
func NewHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
if v, ok := req.Body().(Validator); ok {
if err := v.Validate(); err != nil {
return errors.BadRequest(req.Service(), "%v", err)
}
}
return fn(ctx, req, rsp)
}
}
}