2019-07-07 13:25:20 +03:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/errors"
|
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-07-07 13:25:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|