gpt4 book ai didi

validation - 如何验证字符串 slice

转载 作者:IT王子 更新时间:2023-10-29 01:57:05 24 4
gpt4 key购买 nike

我使用了包“gopkg.in/go-playground/validator.v9”并且我阅读了这个doc .我想验证一段字符串。

There are some points: 1 - Variable must be slice

2 - Max len is 10

3 - Slice should not be null

4 - element slice are string

5 - Max length of every element is 12

这是代码:

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
}

最佳答案

包“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.

所以你可以试试这个:

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)
}

更多细节:

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
}
}

另一种方法是自定义函数验证。在我的测试中,我创建了函数:

//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
}

注册这些函数:

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

结果更优雅(详细):

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
}
}

关于validation - 如何验证字符串 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com