micro-wrapper-validator/validator.go
Vasiliy Tolstov 6eef4cb2e0 use own fork
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-10-02 12:46:05 +03:00

26 lines
558 B
Go

package validator
import (
"context"
"github.com/unistack-org/micro/v3/errors"
"github.com/unistack-org/micro/v3/server"
)
type Validator interface {
Validate() error
}
func NewServerHandlerWrapper() 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)
}
}
}