1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-03-29 20:39:48 +02:00

26 lines
518 B
Go
Raw Normal View History

2020-12-26 15:32:45 +00:00
package validator
import (
"context"
2021-10-12 12:55:53 +01:00
"go-micro.dev/v4/errors"
"go-micro.dev/v4/server"
2020-12-26 15:32:45 +00: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)
}
}
}