...
1 package middleware
2
3 import (
4 "net/http"
5
6 "github.com/gin-gonic/gin"
7 "github.com/gin-gonic/gin/binding"
8
9 "edge-infra.dev/pkg/lib/fog"
10 "edge-infra.dev/pkg/sds/interlock/internal/errors"
11 )
12
13
14
15 func ReadOnlyFieldValidation() gin.HandlerFunc {
16 return func(c *gin.Context) {
17 log := fog.FromContext(c.Request.Context())
18 var body interface{}
19 if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
20 log.Error(err, "failed to validate read-only fields")
21 c.AbortWithStatusJSON(http.StatusInternalServerError, errors.NewErrorResponse(errors.NewError(http.StatusText(http.StatusInternalServerError))))
22 }
23 data := body.(map[string]interface{})
24 if _, ok := data["name"]; ok {
25 detail := errors.NewReadOnlyFieldMessage("Name")
26 c.AbortWithStatusJSON(http.StatusBadRequest, errors.NewErrorResponse(errors.NewError(detail)))
27 }
28 c.Next()
29 }
30 }
31
View as plain text