mirror of
https://github.com/go-micro/go-micro.git
synced 2025-01-17 17:44:30 +02:00
26 lines
518 B
Go
26 lines
518 B
Go
package validator
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go-micro.dev/v4/errors"
|
|
"go-micro.dev/v4/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)
|
|
}
|
|
}
|
|
}
|