小编典典

Golang验证器多字段依赖

go

我想验证以下结构:

type CarModel struct {
  gorm.Model
  OwnerID    int    `json:"ownerid" validate:"nonzero"`
  Type       string `json:"type" validate:"regexp=(?)(A|B)"`
  A        string `json:"url" validate:"isurl"`
  B         string `json:"ip" validate:"isip"`
}

我想根据类型来验证A和B,如果类型= A,则A必须存在并且必须是URL,但是如果类型= B,则B必须不存在,那么A一定不存在,并且B必须是IP

验证程序可以吗?

我确实尝试过自定义验证,但找不到找到类型值的方法:

func checkB(v interface{}, param string) error {
    theB := reflect.ValueOf(v)
    if theB.Kind() != reflect.String {
        return validator.ErrUnsupported
    }
    //check if B is an IP
    ipcool := net.ParseIP(theB.String())
    if ipcool == nil {
        return errors.New("B : ip incorrecte " + theB.String())
    }
    return nil
}

在亚历克斯·尼科尔的回答之后,我首先要感谢您的帮助。

如果我理解正确,则必须遍历所有“验证”字段,以跟踪TYPE,A和B的值,然后根据TYPE进行检查…

我是这样做的:

func checkMonitor(v interface{}) error {
    var mytype string
    var myA string
    var myB string

    val := reflect.ValueOf(v)
    // Iterate through fields
    for i := 0; i < val.NumField(); i++ {
        // Lookup the validate tag
        field := val.Type().Field(i)
        tags := field.Tag
        _, ok := tags.Lookup("validate")
        if !ok {
            // No validate tag.
            continue
        }

        // Get the value of the field.
        fieldValue := val.Field(i)

        switch field.Name {
        case "Type":
            mytype = fieldValue.Interface()
        case "A":
            myA = fieldValue.Interface()
        case "B":
            myB = fieldValue.Interface()
        }
        // Validation logic here.
        //fmt.Println("field", field.Name, "has validate tag", validate, "and value", fieldValue.Interface())
    }
    if mytype == "A" {
        if myA == "" {
            return errors.New("A vide et type A")
        }
        ipcool := net.ParseIP(myA)
        if ipcool == nil {
            return errors.New("A incorrecte " + myA)
        }
    } else if mytype == "HTML" {
        if myB == "" {
            return errors.New("B vide et type B")
        }
        _, urlpascool := url.ParseRequestURI(myB)
        if urlpascool != nil {
            return errors.New("B incorrecte " + myB)
        }
    }
    return nil
}

但是在切换情况下,mytype,myA和myB出现了错误:

不能在分配中使用fieldValue.Interface()(类型接口{})作为类型字符串:需要类型声明

编辑:只需要用我的大脑:

switch field.Name {
case "Type":
   mytype = fieldValue.String()
case "A":
   myA = fieldValue.String()
case "B":
   myB = fieldValue.Interface()
}

阅读 241

收藏
2020-07-02

共1个答案

小编典典

您可能想要使用反射来遍历该结构的字段,获取validate每个字段的标签,然后检查该字段。这意味着您必须在结构级别上进行验证。否则,如果将类似的东西传递myInstance.OwnerID给函数,则会丢失与之关联的标签。

这段代码循环遍历一个结构的字段,并validate为每个字段获取标签:

func checkStruct(v interface{}) error {
    val := reflect.ValueOf(v)

    // Iterate through fields
    for i := 0; i < val.NumField(); i++ {
        // Lookup the validate tag
        field := val.Type().Field(i)
        tags := field.Tag
        validate, ok := tags.Lookup("validate")
        if !ok {
            // No validate tag.
            continue
        }

        // Get the value of the field.
        fieldValue := val.Field(i)

        // Validation logic here.
        fmt.Println("field", field.Name, "has validate tag", validate, "and value",
            fieldValue.Interface())
    }
    return nil
}

例如,我们可以将以下内容传递给它CarModel

checkStruct(CarModel{
    OwnerID: 2,
    Type:    "B",
    A:       "http://google.com",
    B:       "192.168.1.1",
})

它会打印出以下内容:

field OwnerID has validate tag nonzero and value 2
field Type has validate tag regexp=(?)(A|B) and value B
field A has validate tag isurl and value http://google.com
field B has validate tag isip and value 192.168.1.1
2020-07-02