...

Source file src/github.com/go-playground/validator/v10/_examples/dive/main.go

Documentation: github.com/go-playground/validator/v10/_examples/dive

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-playground/validator/v10"
     7  )
     8  
     9  // Test ...
    10  type Test struct {
    11  	Array []string          `validate:"required,gt=0,dive,required"`
    12  	Map   map[string]string `validate:"required,gt=0,dive,keys,keymax,endkeys,required,max=1000"`
    13  }
    14  
    15  // use a single instance of Validate, it caches struct info
    16  var validate *validator.Validate
    17  
    18  func main() {
    19  
    20  	validate = validator.New()
    21  
    22  	// registering alias so we can see the differences between
    23  	// map key, value validation errors
    24  	validate.RegisterAlias("keymax", "max=10")
    25  
    26  	var test Test
    27  
    28  	val(test)
    29  
    30  	test.Array = []string{""}
    31  	test.Map = map[string]string{"test > than 10": ""}
    32  	val(test)
    33  }
    34  
    35  func val(test Test) {
    36  	fmt.Println("testing")
    37  	err := validate.Struct(test)
    38  	fmt.Println(err)
    39  }
    40  

View as plain text