-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(binding): add support for custom validator / validation tags (#1068
) * feat(binding): Add support for custom validation tags * docs: Add example for custom validation tag * test(binding): Add test for registering custom validation
- Loading branch information
1 parent
030b1aa
commit 26c3f42
Showing
5 changed files
with
175 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
validator "gopkg.in/go-playground/validator.v8" | ||
) | ||
|
||
type Booking struct { | ||
CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"` | ||
CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"` | ||
} | ||
|
||
func bookableDate( | ||
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, | ||
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string, | ||
) bool { | ||
if date, ok := field.Interface().(time.Time); ok { | ||
today := time.Now() | ||
if today.Year() > date.Year() || today.YearDay() > date.YearDay() { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func main() { | ||
route := gin.Default() | ||
binding.Validator.RegisterValidation("bookabledate", bookableDate) | ||
route.GET("/bookable", getBookable) | ||
route.Run(":8085") | ||
} | ||
|
||
func getBookable(c *gin.Context) { | ||
var b Booking | ||
if err := c.ShouldBindWith(&b, binding.Query); err == nil { | ||
c.JSON(http.StatusOK, gin.H{"message": "Booking dates are valid!"}) | ||
} else { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
} | ||
} |