diff --git a/backend/lib/validate/mongo.go b/backend/lib/validate/mongo.go new file mode 100644 index 00000000..7ef94177 --- /dev/null +++ b/backend/lib/validate/mongo.go @@ -0,0 +1,10 @@ +package validate + +import ( + "github.com/globalsign/mgo/bson" + "github.com/go-playground/validator/v10" +) + +func MongoID(sl validator.FieldLevel) bool { + return bson.IsObjectIdHex(sl.Field().String()) +} diff --git a/backend/main.go b/backend/main.go index 6d453e6f..458856c7 100644 --- a/backend/main.go +++ b/backend/main.go @@ -5,6 +5,7 @@ import ( "crawlab/config" "crawlab/database" _ "crawlab/docs" + validate2 "crawlab/lib/validate" "crawlab/middlewares" "crawlab/model" "crawlab/routes" @@ -13,6 +14,8 @@ import ( "crawlab/services/rpc" "github.com/apex/log" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "github.com/go-playground/validator/v10" "github.com/olivere/elastic/v7" "github.com/spf13/viper" "github.com/swaggo/gin-swagger" @@ -34,6 +37,10 @@ func init() { func main() { app := gin.New() app.Use(gin.Logger(), gin.Recovery()) + if v, ok := binding.Validator.Engine().(*validator.Validate); ok { + _ = v.RegisterValidation("bid", validate2.MongoID) + } + if swagHandler != nil { app.GET("/swagger/*any", swagHandler) }