aboutsummaryrefslogtreecommitdiff
path: root/routes.go
diff options
context:
space:
mode:
Diffstat (limited to 'routes.go')
-rw-r--r--routes.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/routes.go b/routes.go
index 3759382..37088e2 100644
--- a/routes.go
+++ b/routes.go
@@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
log "github.com/sirupsen/logrus"
+ "github.com/go-playground/validator/v10"
"net/http"
)
@@ -13,17 +13,38 @@ func ApplicationRoutes(engine *gin.Engine) {
api := engine.Group("/api")
{
- api.POST("/rules", func(c *gin.Context) {
+ api.POST("/rule", func(c *gin.Context) {
var rule Rule
+ //data, _ := c.GetRawData()
+ //
+ //var json = jsoniter.ConfigCompatibleWithStandardLibrary
+ //err := json.Unmarshal(data, &filter)
+ //
+ //if err != nil {
+ // log.WithError(err).Error("failed to unmarshal")
+ // c.String(500, "failed to unmarshal")
+ //}
+ //
+ //err = validator.New().Struct(filter)
+ //log.WithError(err).WithField("filter", filter).Error("aaaa")
+ //c.String(200, "ok")
+
if err := c.ShouldBindJSON(&rule); err != nil {
- for _, fieldErr := range err.(validator.ValidationErrors) {
+ validationErrors, ok := err.(validator.ValidationErrors)
+ if !ok {
+ log.WithError(err).WithField("rule", rule).Error("oops")
+ c.JSON(http.StatusBadRequest, gin.H{})
+ return
+ }
+
+ for _, fieldErr := range validationErrors {
log.Println(fieldErr)
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("field '%v' does not respect the %v(%v) rule",
fieldErr.Field(), fieldErr.Tag(), fieldErr.Param()),
})
- log.WithError(err).WithField("rule", rule).Panic("oops")
+ log.WithError(err).WithField("rule", rule).Error("oops")
return // exit on first error
}
}