我使用了包“gopkg.in/go-playground/validator.v9”,我读了这个doc。 我想验证一段字符串。
有一些要点:1 - 变量必须是切片
2 - Max len是10
3 - 切片不应该为空
4 - 元素切片是字符串
5 - 每个元素的最大长度为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
}
答案 0 :(得分:3)
包“gopkg.in/go-playground/validator.v9”用于结构和变量,看:
包验证器实现结构和值的值验证 基于标签的单个字段。
它还可以处理嵌套的跨领域和跨结构验证 结构,并能够潜入任何类型的数组和地图。
所以你可以试试这个:
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
}
}