commit 540e370613a11fbf52b462cf2cb28964823f77e7 Author: Vasiliy Tolstov Date: Sun Jul 7 13:25:20 2019 +0300 add validator wrapper (#370) all requests that have method Validate() error can be used to validate incoming request Signed-off-by: Vasiliy Tolstov diff --git a/validator.go b/validator.go new file mode 100644 index 0000000..7457d3e --- /dev/null +++ b/validator.go @@ -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) + } + } +}