main.go 765 B

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