generated from WildEgor/e-shop-fiber-microservice-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
74 lines (61 loc) · 1.86 KB
/
validator.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package validators
import (
core_dtos "github.com/WildEgor/e-shop-gopack/pkg/core/dtos"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v3"
"github.com/google/uuid"
"net/mail"
)
// NewValidator func for create a new validator for model fields.
func NewValidator() *validator.Validate {
// Create a new validator for a Book model.
validate := validator.New()
// Custom validation for uuid.UUID fields.
_ = validate.RegisterValidation("uuid", func(fl validator.FieldLevel) bool {
field := fl.Field().String()
if _, err := uuid.Parse(field); err != nil {
return true
}
return false
})
// Custom validation for Emails fields.
_ = validate.RegisterValidation("email", func(fl validator.FieldLevel) bool {
field := fl.Field().String()
if _, err := mail.ParseAddress(field); err != nil {
return true
}
return false
})
return validate
}
// ValidatorErrors func for show validation errors for each invalid fields.
func ValidatorErrors(err error) map[string]string {
// Define fields map.
fields := map[string]string{}
// FIXME
//if err != nil {
// // Make error message for each invalid field.
// for _, err := range err.(validator.ValidationErrors) {
// fields[err.Field()] = err.Error()
// }
//}
return fields
}
// ParseAndValidate parser
func ParseAndValidate(ctx fiber.Ctx, out interface{}) error {
resp := core_dtos.NewResponse(ctx)
// Checking received data from JSON body. Return status 400 and error message.
if err := ctx.Bind().Body(&out); err != nil {
resp.SetStatus(fiber.StatusBadRequest)
resp.SetMessage(err.Error())
return resp.JSON()
}
// Create a new validator for a RegistrationRequestDto.
validate := NewValidator()
// Validate fields.
if err := validate.Struct(&out); err != nil {
resp.SetStatus(fiber.StatusBadRequest)
resp.SetMessage(err.Error()) // ValidatorErrors(err)
}
return resp.JSON()
}