examples_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package validator_test
  2. // import (
  3. // "fmt"
  4. // "gopkg.in/go-playground/validator.v8"
  5. // )
  6. // func ExampleValidate_new() {
  7. // config := &validator.Config{TagName: "validate"}
  8. // validator.New(config)
  9. // }
  10. // func ExampleValidate_field() {
  11. // // This should be stored somewhere globally
  12. // var validate *validator.Validate
  13. // config := &validator.Config{TagName: "validate"}
  14. // validate = validator.New(config)
  15. // i := 0
  16. // errs := validate.Field(i, "gt=1,lte=10")
  17. // err := errs.(validator.ValidationErrors)[""]
  18. // fmt.Println(err.Field)
  19. // fmt.Println(err.Tag)
  20. // fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time
  21. // fmt.Println(err.Type)
  22. // fmt.Println(err.Param)
  23. // fmt.Println(err.Value)
  24. // //Output:
  25. // //
  26. // //gt
  27. // //int
  28. // //int
  29. // //1
  30. // //0
  31. // }
  32. // func ExampleValidate_struct() {
  33. // // This should be stored somewhere globally
  34. // var validate *validator.Validate
  35. // config := &validator.Config{TagName: "validate"}
  36. // validate = validator.New(config)
  37. // type ContactInformation struct {
  38. // Phone string `validate:"required"`
  39. // Street string `validate:"required"`
  40. // City string `validate:"required"`
  41. // }
  42. // type User struct {
  43. // Name string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,)
  44. // Age int8 `validate:"required,gt=0,lt=150"`
  45. // Email string `validate:"email"`
  46. // ContactInformation []*ContactInformation
  47. // }
  48. // contactInfo := &ContactInformation{
  49. // Street: "26 Here Blvd.",
  50. // City: "Paradeso",
  51. // }
  52. // user := &User{
  53. // Name: "Joey Bloggs",
  54. // Age: 31,
  55. // Email: "joeybloggs@gmail.com",
  56. // ContactInformation: []*ContactInformation{contactInfo},
  57. // }
  58. // errs := validate.Struct(user)
  59. // for _, v := range errs.(validator.ValidationErrors) {
  60. // fmt.Println(v.Field) // Phone
  61. // fmt.Println(v.Tag) // required
  62. // //... and so forth
  63. // //Output:
  64. // //Phone
  65. // //required
  66. // }
  67. // }