add validator middleware

pull/711/head
chenzhihui 4 years ago
parent c7827ff701
commit ad97e72c15
  1. 26
      middleware/validate/validate.go

@ -0,0 +1,26 @@
package validate
import (
"context"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
)
type validator interface {
Validate() error
}
// Validator is a validator middleware.
func Validator() middleware.Middleware {
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if v, ok := req.(validator); ok {
if err := v.Validate(); err != nil {
return nil, errors.InvalidArgument("Validator", err.Error())
}
}
return handler(ctx, req)
}
}
}
Loading…
Cancel
Save