7

I used package "gopkg.in/go-playground/validator.v9" and I read this doc . I want to validate for a slice of string.

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

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

1 Answer 1

11

Package "gopkg.in/go-playground/validator.v9" is for structs and variables, look:

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
    }
}
1
  • Thank you so much for the time you responded. @Aristofanio Garcia Commented Apr 29, 2018 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.