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.
|
|
|
package validate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.diulo.com/mogfee/kit/middleware"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Server() middleware.Middleware {
|
|
|
|
return func(handler middleware.Handler) middleware.Handler {
|
|
|
|
return func(ctx context.Context, a any) (any, error) {
|
|
|
|
if v, ok := a.(interface {
|
|
|
|
Validate() error
|
|
|
|
}); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
if a, ok := err.(interface {
|
|
|
|
Field() string
|
|
|
|
Reason() string
|
|
|
|
}); ok {
|
|
|
|
field := a.Field()
|
|
|
|
return nil, errorx.BadRequest("InvalidArgument", "").WithMetadata(map[string]string{
|
|
|
|
strings.ToLower(field[0:1]) + field[1:]: a.Reason(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handler(ctx, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|