Open jihadismail8 opened 2 days ago
You can combine multiple validators in a single route using the append function and variadic arguments.
package main
import (
"net/http"
gv "github.com/bube054/ginvalidator"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
createEmailValidator := func() gin.HandlerFunc {
return gv.NewBody("email", nil).Chain().Email(nil).Validate()
}
createNameValidator := func() gin.HandlerFunc {
return gv.NewBody("name", nil).Chain().Not().Empty(nil).Validate()
}
combineValidators := func() []gin.HandlerFunc {
return []gin.HandlerFunc{
createEmailValidator(),
createNameValidator(),
}
}
r.GET("/hello", append(combineValidators(), func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"message": "Hello World!",
})
})...)
r.Run()
}
Well though it works, the above code is not idiomatic go code. With this technique you should be able to group validators together.
Does this answer your question? Also don't forget to please star and share.
how to combine multi validations in one route ? i have this route
g.POST("/", Validators.Name(),Validators.IpAddress(), h.Create)
i would like instead of calling multiple functions for the route , i want to make one function that would call them all . like thisso how to do that ? any idea , could not find anything in docs , but this is useful , because i could use for example the Name validator in another route or something