Vasiliy Tolstov 540e370613 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>
2020-10-02 12:41:46 +03:00

26 lines
538 B
Go

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)
}
}
}