59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"jannex/admin-dashboard-backend/modules/structs"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
FailedField string
|
|
Tag string
|
|
Value string
|
|
}
|
|
|
|
var Validate = validator.New()
|
|
|
|
func ValidateStruct(event interface{}) []*ErrorResponse {
|
|
var errors []*ErrorResponse
|
|
err := Validate.Struct(event)
|
|
if err != nil {
|
|
for _, err := range err.(validator.ValidationErrors) {
|
|
var element ErrorResponse
|
|
element.FailedField = err.StructNamespace()
|
|
element.Tag = err.Tag()
|
|
element.Value = err.Param()
|
|
errors = append(errors, &element)
|
|
}
|
|
}
|
|
return errors
|
|
}
|
|
|
|
func validateNumericString(fl validator.FieldLevel) bool {
|
|
str := fl.Field().String()
|
|
return strings.TrimSpace(str) != "" && regexp.MustCompile(`^[0-9]+$`).MatchString(str)
|
|
}
|
|
|
|
func ValidatorInit() {
|
|
Validate.RegisterValidation("numericstring", validateNumericString)
|
|
|
|
Validate.RegisterStructValidationMapRules(generalRules,
|
|
structs.UserLoginRequest{},
|
|
structs.UserSignOutSessionRequest{},
|
|
structs.ScannerRequest{})
|
|
|
|
Validate.RegisterStructValidationMapRules(groupTaskRules,
|
|
structs.ApiGroupTaskRequest{})
|
|
|
|
Validate.RegisterStructValidationMapRules(equipmentRules,
|
|
structs.EquipmentRequest{},
|
|
structs.GetDocumentationEquipmentRequest{},
|
|
structs.CreateEquipmentDocumentationRequest{},
|
|
structs.EditEquipmentDocumentationRequest{})
|
|
|
|
Validate.RegisterStructValidationMapRules(notificationRules,
|
|
structs.AddNotificationRequest{})
|
|
}
|