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.
32 lines
716 B
32 lines
716 B
2 years ago
|
package validate
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.diulo.com/mogfee/kit/errors"
|
||
|
"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, errors.BadRequest("InvalidArgument", "").WithMetadata(map[string]string{
|
||
|
strings.ToLower(field[0:1]) + field[1:]: a.Reason(),
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return handler(ctx, a)
|
||
|
}
|
||
|
}
|
||
|
}
|