简体   繁体   English

如何验证字符串切片

[英]How to validate slice of string

I used package "gopkg.in/go-playground/validator.v9" and I read this doc . 我使用了“ gopkg.in/go-playground/validator.v9”包,并阅读了该文档 I want to validate for a slice of string. 我想验证一个字符串切片。

There are some points: 1 - Variable must be slice 有几点:1-变量必须是切片

2 - Max len is 10 2-最大len是10

3 - Slice should not be null 3-Slice不应该为null

4 - element slice are string 4-元素切片为字符串

5 - Max length of every element is 12 5-每个元素的最大长度为12

This is code : 这是代码:

var validate *validator.Validate

func main() {
    validate = validator.New()
    validateVariable()
}

func validateVariable() {
mySlice := []string{"111","222"}
errs := validate.Var(mySlice , "required,max=10,min=1")
if errs != nil {
    fmt.Println(errs) 
    return
}

Package "gopkg.in/go-playground/validator.v9" is for structs and variables, look: 软件包“ gopkg.in/go-playground/validator.v9”用于结构和变量,请看:

Package validator implements value validations for structs and individual fields based on tags. 包验证器基于标签对结构和单个字段实现值验证。

It can also handle Cross-Field and Cross-Struct validation for nested structs and has the ability to dive into arrays and maps of any type. 它还可以处理嵌套结构的跨字段和跨结构验证,并具有深入研究任何类型的数组和映射的能力。

So you can try this: 因此,您可以尝试以下操作:

func validateVariable() {
    mySlice := []string{"111", "222"}
    //1 (to convert in error)
    println(reflect.TypeOf(mySlice).Kind() == reflect.Slice)
    //2,3,5
    errs := validate.Var(mySlice, "required,max=10,min=1,dive,max=12")
    if errs != nil {
        fmt.Println(errs)
        return
    }
    //4  (to convert in error)
    println(reflect.TypeOf(mySlice).Elem().Kind() == reflect.String)
}

With more details: 详细信息:

func validateVariable() {
    mySlice := []string{"111", "222"}
    //mySlice := []string{"111", "222", "", "", "", "", "", "", "", "", "", "", ""}
    //mySlice := []string{"111", "222000000000000", "0000000000000"}
    //1
    if reflect.TypeOf(mySlice).Kind() != reflect.Slice {
        fmt.Println("Invalid in: Variable must be slice")
    }
    //2
    errs2 := validate.Var(mySlice, "max=10")
    if errs2 != nil {
        fmt.Println("Invalid in: Max len is 10. Original: ")
        fmt.Println(errs2)
        return
    }
    //3
    errs3 := validate.Var(mySlice, "required")
    if errs3 != nil {
        fmt.Println("Invalid in: Slice should not be null. Original: ")
        fmt.Println(errs3)
        return
    }
    //4
    if reflect.TypeOf(mySlice).Elem().Kind() != reflect.String {
        fmt.Println("Invalid in: Element slice are string")
        return
    }
    //5
    errs5 := validate.Var(mySlice, "dive,max=12") //applied in elements
    if errs5 != nil {
        fmt.Println("Invalid in: Max length of every element is 12. Original: ")
        fmt.Println(errs5)
        return
    }
}

Another way is custom func validation. 另一种方法是自定义功能验证。 In my test I was create the functions: 在测试中,我创建了以下函数:

//IsSlice check if field kind is equal to slice
func IsSlice(fl validator.FieldLevel) bool {
    if fl.Top().Kind() == reflect.Slice {
        return true
    }
    return false
}

//IsSlice check if field element kind is equal to string
func IsStringElem(fl validator.FieldLevel) bool {
    t := fl.Top().Type()
    if t.Elem().Kind() == reflect.String {
        return true
    }
    return false
}

Registry these functions: 注册这些功能:

func main() {
    validate = validator.New()
    validate.RegisterValidation("isslice", IsSlice)
    validate.RegisterValidation("isstringelem", IsStringElem)
    validateVariable()
}

The result is more elegant (in detail): 结果更加优雅(详细):

func validateVariable() {
    mySlice := []string{"111", "222"}
    //mySlice := []string{"111", "222", "", "", "", "", "", "", "", "", "", "", ""}
    //mySlice := []string{"111", "222000000000000", "0000000000000"}
    //1 - using tag custimized
    errs1 := validate.Var(mySlice, "isslice")
    if errs1 != nil {
        fmt.Println("Invalid in: Variable must be slice")
        fmt.Println(errs1)
        return
    }
    //2
    errs2 := validate.Var(mySlice, "max=10")
    if errs2 != nil {
        fmt.Println("Invalid in: Max len is 10. Original: ")
        fmt.Println(errs2)
        return
    }
    //3
    errs3 := validate.Var(mySlice, "required")
    if errs3 != nil {
        fmt.Println("Invalid in: Slice should not be null. Original: ")
        fmt.Println(errs3)
        return
    }
    //4 - using tag customized
    errs4 := validate.Var(mySlice, "isstringelem")
    if errs4 != nil {
        fmt.Println("Invalid in: Element slice are string")
        fmt.Println(errs4)
        return
    }
    //5
    errs5 := validate.Var(mySlice, "dive,max=12") //applied in elements
    if errs5 != nil {
        fmt.Println("Invalid in: Max length of every element is 12. Original: ")
        fmt.Println(errs5)
        return
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM