Skip to content

Commit

Permalink
fix: validator bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotyu committed Dec 12, 2023
1 parent d32e32a commit 5e8069f
Showing 1 changed file with 3 additions and 27 deletions.
30 changes: 3 additions & 27 deletions src/makeline-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)

var validate *validator.Validate

// Valid database API types
const (
AZURE_COSMOS_DB_SQL_API = "cosmosdbsql"
Expand Down Expand Up @@ -105,23 +102,16 @@ func getOrder(c *gin.Context) {
return
}

err := validate.Var(c.Param("id"), "required,numeric")
if err != nil {
log.Printf("Failed to validate order id: %s", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Printf("Failed to convert order id to int: %s", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}

orderId := strconv.FormatInt(int64(id), 10)
sanitizedOrderId := strconv.FormatInt(int64(id), 10)

order, err := client.repo.GetOrder(orderId)
order, err := client.repo.GetOrder(sanitizedOrderId)
if err != nil {
log.Printf("Failed to get order from database: %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand All @@ -148,21 +138,7 @@ func updateOrder(c *gin.Context) {
return
}

err := validate.Struct(order)
validationErrors := err.(validator.ValidationErrors)
if err != nil {
log.Printf("Failed to validate order: %s", validationErrors)
c.AbortWithStatus(http.StatusBadRequest)
return
}
err = validate.Var(order.OrderID, "required,numeric")
if err != nil {
log.Printf("Failed to validate order id: %s", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}

id, err := strconv.Atoi(c.Param("id"))
id, err := strconv.Atoi(order.OrderID)
if err != nil {
log.Printf("Failed to convert order id to int: %s", err)
c.AbortWithStatus(http.StatusBadRequest)
Expand Down

0 comments on commit 5e8069f

Please sign in to comment.