main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/go-playground/validator.v9"
  5. )
  6. // User contains user information
  7. type User struct {
  8. FirstName string `json:"fname"`
  9. LastName string `json:"lname"`
  10. Age uint8 `validate:"gte=0,lte=130"`
  11. Email string `validate:"required,email"`
  12. FavouriteColor string `validate:"hexcolor|rgb|rgba"`
  13. Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
  14. }
  15. // Address houses a users address information
  16. type Address struct {
  17. Street string `validate:"required"`
  18. City string `validate:"required"`
  19. Planet string `validate:"required"`
  20. Phone string `validate:"required"`
  21. }
  22. // use a single instance of Validate, it caches struct info
  23. var validate *validator.Validate
  24. func main() {
  25. validate = validator.New()
  26. // register validation for 'User'
  27. // NOTE: only have to register a non-pointer type for 'User', validator
  28. // interanlly dereferences during it's type checks.
  29. validate.RegisterStructValidation(UserStructLevelValidation, User{})
  30. // build 'User' info, normally posted data etc...
  31. address := &Address{
  32. Street: "Eavesdown Docks",
  33. Planet: "Persphone",
  34. Phone: "none",
  35. City: "Unknown",
  36. }
  37. user := &User{
  38. FirstName: "",
  39. LastName: "",
  40. Age: 45,
  41. Email: "Badger.Smith@gmail.com",
  42. FavouriteColor: "#000",
  43. Addresses: []*Address{address},
  44. }
  45. // returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError )
  46. err := validate.Struct(user)
  47. if err != nil {
  48. // this check is only needed when your code could produce
  49. // an invalid value for validation such as interface with nil
  50. // value most including myself do not usually have code like this.
  51. if _, ok := err.(*validator.InvalidValidationError); ok {
  52. fmt.Println(err)
  53. return
  54. }
  55. for _, err := range err.(validator.ValidationErrors) {
  56. fmt.Println(err.Namespace())
  57. fmt.Println(err.Field())
  58. fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
  59. fmt.Println(err.StructField()) // by passing alt name to ReportError like below
  60. fmt.Println(err.Tag())
  61. fmt.Println(err.ActualTag())
  62. fmt.Println(err.Kind())
  63. fmt.Println(err.Type())
  64. fmt.Println(err.Value())
  65. fmt.Println(err.Param())
  66. fmt.Println()
  67. }
  68. // from here you can create your own error messages in whatever language you wish
  69. return
  70. }
  71. // save user to database
  72. }
  73. // UserStructLevelValidation contains custom struct level validations that don't always
  74. // make sense at the field validation level. For Example this function validates that either
  75. // FirstName or LastName exist; could have done that with a custom field validation but then
  76. // would have had to add it to both fields duplicating the logic + overhead, this way it's
  77. // only validated once.
  78. //
  79. // NOTE: you may ask why wouldn't I just do this outside of validator, because doing this way
  80. // hooks right into validator and you can combine with validation tags and still have a
  81. // common error output format.
  82. func UserStructLevelValidation(sl validator.StructLevel) {
  83. user := sl.Current().Interface().(User)
  84. if len(user.FirstName) == 0 && len(user.LastName) == 0 {
  85. sl.ReportError(user.FirstName, "FirstName", "fname", "fnameorlname", "")
  86. sl.ReportError(user.LastName, "LastName", "lname", "fnameorlname", "")
  87. }
  88. // plus can do more, even with different tag than "fnameorlname"
  89. }