-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
50 lines (40 loc) · 970 Bytes
/
Copy pathutils.go
File metadata and controls
50 lines (40 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fiberkit
import (
"errors"
"reflect"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v3"
)
func validateInput(value any) error {
typ := reflect.TypeOf(value)
if typ == nil {
return nil
}
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil
}
return validator.New().Struct(value)
}
func invalidInput(ctx fiber.Ctx, target string) error {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid " + target,
})
}
func invalidValidation(ctx fiber.Ctx, err error) error {
payload := fiber.Map{
"error": "validation failed",
}
if validationErrs, ok := errors.AsType[validator.ValidationErrors](err); ok {
details := fiber.Map{}
for _, fieldErr := range validationErrs {
details[fieldErr.Field()] = fieldErr.Tag()
}
if len(details) > 0 {
payload["details"] = details
}
}
return ctx.Status(fiber.StatusBadRequest).JSON(payload)
}