zoukankan      html  css  js  c++  java
  • gin内置验证器使用

    gin内置验证器使用

    func TopicUrl(f1 validator.FieldLevel) bool {
        return true //返回true表示验证成功
    }
    
    func main(){
        router:=gin.Default()
        if v, ok := binding.Validator.Engine().(*validator.Validate); ok {  //类型断言
            v.RegisterValidation("topicurl", TopicUrl) //注册调用tag和自定义验证器
        }
        v1:=router.Group("/v1/topics")
        {
            v1.GET("/:topic_id",GetTopicDetail)
        }
    }
    
    type Topic struct {
        TopicID         int    `form:"id" json:"id"`
        TopicTitle      string `form:"title" json:"title" binding:"min=4,max=20"` //长度4到20之间
        TopicShortTitle string `form:"stitle" json:"stitle" binding:"required,nefield=ToipcTitle"`//required非空,nfield不能等于TopicTitle字段
        UserIP          string `form:"ip" json:"ip" binding:"ipv4"`
        TopicScore      int    `form:"score" json:"score" binding:"omitempty,gt=5"` //omitempty要么不传,传的话就要大于5
    }
    
    type Topic struct {
        TopicID         int    `form:"id" json:"id"`
        TopicTitle      string `form:"title" json:"title" binding:"min=4,max=20"`
        TopicShortTitle string `form:"stitle" json:"stitle" binding:"required,nefield=TopicTitle"`
        UserIP          string `form:"ip" json:"ip" binding:"ipv4"`
        TopicScore      int    `form:"score" json:"score" binding:"omitempty,gt=5"`
        TopicUrl        string `form:"url" json:"url" binding:"topicurl"` //绑定自定义的topicurl验证器
    }
    
    type Topics struct {
        TopicList [] *Topic `form:"topiclist" json:"topiclist" binding:"gt=0,lt=3,dive"` //dive表示进入列表或数组里面的字段去验证,这里是验证上面的Topic,dive要写在后面,不然如果lt写在dive后面就是验证里面的数据的lt>3,而不是列表的lt>3
        Size      int       `form:"size" json:"size"`
    }




  • 相关阅读:
    [Cerc2013]Magical GCD
    UVA 1393 Highways
    UVA 10214 Trees in a Wood
    [SDOI2010]大陆争霸
    Oracle逻辑读详解
    DBA_2PC_PENDING (转)
    oracle autotrace使用
    升级oracle 9i到10g
    VMware 虚拟机中添加新硬盘的方法(转载)
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql (转)
  • 原文地址:https://www.cnblogs.com/hualou/p/12070821.html
Copyright © 2011-2022 走看看