add validator

This commit is contained in:
kandrusyak
2023-08-02 01:51:27 +03:00
parent c55df47ada
commit 3a514800e6
6 changed files with 59 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ import (
"astra-events/model"
"astra-events/pkg/utils"
"astra-events/services"
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/go-set"
"github.com/labstack/echo/v4"
@@ -19,8 +20,11 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
return c.NoContent(http.StatusUnauthorized)
}
if err = c.Bind(event); err != nil {
return c.NoContent(http.StatusBadRequest)
err = c.Bind(&event)
err = h.Validator.Struct(event)
if err != nil {
return c.HTML(http.StatusBadRequest, fmt.Sprintf("Err(s):\n%+v\n", err))
}
if event.Person.ID.ID() != 0 {

View File

@@ -1,7 +1,11 @@
package handler
import "gorm.io/gorm"
import (
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)
type Handler struct {
DB *gorm.DB
DB *gorm.DB
Validator *validator.Validate
}