chore: add request validations

This commit is contained in:
2025-06-03 20:23:06 +01:00
parent e00cefb8e4
commit 0879aec750
6 changed files with 67 additions and 15 deletions
+2
View File
@@ -1,6 +1,7 @@
package api
import (
"github.com/go-playground/validator/v10"
"github.com/hazemKrimi/crimson-vault/internal/models"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@@ -14,6 +15,7 @@ type API struct {
func (api *API) Initialize() {
db := &models.DB{}
ech := echo.New()
ech.Validator = &CustomValidator{validator: validator.New(validator.WithRequiredStructEnabled())}
db.Connect()
db.MigrateClients()
+10 -2
View File
@@ -11,13 +11,17 @@ import (
)
func (api *API) CreateClientHandler(context echo.Context) error {
var body models.CreateClientBody
var body models.CreateClientRequestBody
if err := context.Bind(&body); err != nil {
log.Println(fmt.Sprintf("Error creating Client: %v.", err))
return context.String(http.StatusBadRequest, "Invalid JSON!")
}
if err := context.Validate(body); err != nil {
return err
}
client := api.db.CreateClient(body)
log.Println(fmt.Sprintf("Client created with ID %d.", client.ID))
@@ -71,13 +75,17 @@ func (api *API) UpdateClientHandler(context echo.Context) error {
return context.String(http.StatusInternalServerError, "Unexpected error updating Client!")
}
var body models.UpdateClientBody
var body models.UpdateClientRequestBody
if err := context.Bind(&body); err != nil {
log.Println(fmt.Sprintf("Error updating Client: %v.", err))
return context.String(http.StatusBadRequest, "Invalid JSON!")
}
if err := context.Validate(body); err != nil {
return err
}
var client models.Client
if err := api.db.UpdateClient(id, body, &client); err != nil {
+19
View File
@@ -0,0 +1,19 @@
package api
import (
"net/http"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
)
type CustomValidator struct {
validator *validator.Validate
}
func (validator *CustomValidator) Validate(i any) error {
if err := validator.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return nil
}
+21 -13
View File
@@ -20,31 +20,31 @@ type Client struct {
Email string `json:"email"`
}
type CreateClientBody struct {
Name string `json:"name"`
type CreateClientRequestBody struct {
Name string `json:"name" validate:"required"`
FiscalCode string `json:"fiscalCode"`
Address string `json:"address"`
Zip string `json:"zip"`
Country string `json:"country"`
Phone string `json:"phone"`
Email string `json:"email"`
Address string `json:"address" validate:"required"`
Zip string `json:"zip" validate:"required"`
Country string `json:"country" validate:"required"`
Phone string `json:"phone" validate:"required,e164"`
Email string `json:"email" validate:"required,email"`
}
type UpdateClientBody struct {
type UpdateClientRequestBody struct {
Name string `json:"name"`
FiscalCode string `json:"fiscalCode"`
Address string `json:"address"`
Zip string `json:"zip"`
Country string `json:"country"`
Phone string `json:"phone"`
Email string `json:"email"`
Phone string `json:"phone" validate:"omitempty,e164"`
Email string `json:"email" validate:"omitempty,email"`
}
func (db *DB) MigrateClients() {
db.instance.AutoMigrate(&Client{})
}
func (db *DB) CreateClient(body CreateClientBody) Client {
func (db *DB) CreateClient(body CreateClientRequestBody) Client {
client := Client{Name: body.Name, Country: body.Country, Phone: body.Phone}
db.instance.Create(&client)
@@ -73,14 +73,22 @@ func (db *DB) GetClient(id int, client *Client) error {
return nil
}
func (db *DB) UpdateClient(id int, body UpdateClientBody, client *Client) error {
func (db *DB) UpdateClient(id int, body UpdateClientRequestBody, client *Client) error {
result := db.instance.Where("id = ?", id).First(&client, id)
if result.Error != nil {
return result.Error
}
result = db.instance.Model(&client).Updates(Client{Name: body.Name, Country: body.Country, Phone: body.Phone})
result = db.instance.Model(&client).Updates(Client{
Name: body.Name,
FiscalCode: body.FiscalCode,
Address: body.Address,
Zip: body.Zip,
Country: body.Country,
Phone: body.Phone,
Email: body.Email,
})
if result.Error != nil {
return result.Error