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
612 B
26 lines
612 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(domain string) 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(domain, "validator", err.Error())
|
|
}
|
|
}
|
|
return handler(ctx, req)
|
|
}
|
|
}
|
|
}
|
|
|