You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
596 B
27 lines
596 B
4 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|