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.
26 lines
606 B
26 lines
606 B
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.BadRequest("VALIDATOR", err.Error()).WithCause(err) |
|
} |
|
} |
|
return handler(ctx, req) |
|
} |
|
} |
|
}
|
|
|