example_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package valid_test
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "regexp"
  7. "kpt-tmr-group/pkg/valid"
  8. "kpt-tmr-group/pkg/valid/is"
  9. )
  10. type Address struct {
  11. Street string
  12. City string
  13. State string
  14. Zip string
  15. }
  16. type Customer struct {
  17. Name string
  18. Gender string
  19. Email string
  20. Address Address
  21. }
  22. func (a Address) Validate() error {
  23. return valid.ValidateStruct(&a,
  24. // Street cannot be empty, and the length must between 5 and 50
  25. valid.Field(&a.Street, valid.Required, valid.Length(5, 50)),
  26. // City cannot be empty, and the length must between 5 and 50
  27. valid.Field(&a.City, valid.Required, valid.Length(5, 50)),
  28. // State cannot be empty, and must be a string consisting of two letters in upper case
  29. valid.Field(&a.State, valid.Required, valid.Match(regexp.MustCompile("^[A-Z]{2}$"))),
  30. // State cannot be empty, and must be a string consisting of five digits
  31. valid.Field(&a.Zip, valid.Required, valid.Match(regexp.MustCompile("^[0-9]{5}$"))),
  32. )
  33. }
  34. func (c Customer) Validate() error {
  35. return valid.ValidateStruct(&c,
  36. // Name cannot be empty, and the length must be between 5 and 20.
  37. valid.Field(&c.Name, valid.Required, valid.Length(5, 20)),
  38. // Gender is optional, and should be either "Female" or "Male".
  39. valid.Field(&c.Gender, valid.In("Female", "Male")),
  40. // Email cannot be empty and should be in a valid email format.
  41. valid.Field(&c.Email, valid.Required, is.Email),
  42. // Validate Address using its own validation rules
  43. valid.Field(&c.Address),
  44. )
  45. }
  46. func Example() {
  47. c := Customer{
  48. Name: "Qiang Xue",
  49. Email: "q",
  50. Address: Address{
  51. Street: "123 Main Street",
  52. City: "Unknown",
  53. State: "Virginia",
  54. Zip: "12345",
  55. },
  56. }
  57. err := c.Validate()
  58. fmt.Println(err)
  59. // Output:
  60. // Address: (State: must be in a valid format.); Email: must be a valid email address.
  61. }
  62. func Example_second() {
  63. data := "example"
  64. err := valid.Validate(data,
  65. valid.Required, // not empty
  66. valid.Length(5, 100), // length between 5 and 100
  67. is.URL, // is a valid URL
  68. )
  69. fmt.Println(err)
  70. // Output:
  71. // must be a valid URL
  72. }
  73. func Example_third() {
  74. addresses := []Address{
  75. {State: "MD", Zip: "12345"},
  76. {Street: "123 Main St", City: "Vienna", State: "VA", Zip: "12345"},
  77. {City: "Unknown", State: "NC", Zip: "123"},
  78. }
  79. err := valid.Validate(addresses)
  80. fmt.Println(err)
  81. // Output:
  82. // 0: (City: cannot be blank; Street: cannot be blank.); 2: (Street: cannot be blank; Zip: must be in a valid format.).
  83. }
  84. func Example_four() {
  85. c := Customer{
  86. Name: "Qiang Xue",
  87. Email: "q",
  88. Address: Address{
  89. State: "Virginia",
  90. },
  91. }
  92. err := valid.Errors{
  93. "name": valid.Validate(c.Name, valid.Required, valid.Length(5, 20)),
  94. "email": valid.Validate(c.Name, valid.Required, is.Email),
  95. "zip": valid.Validate(c.Address.Zip, valid.Required, valid.Match(regexp.MustCompile("^[0-9]{5}$"))),
  96. }.Filter()
  97. fmt.Println(err)
  98. // Output:
  99. // email: must be a valid email address; zip: cannot be blank.
  100. }
  101. func Example_five() {
  102. type Employee struct {
  103. Name string
  104. }
  105. type Manager struct {
  106. Employee
  107. Level int
  108. }
  109. m := Manager{}
  110. err := valid.ValidateStruct(&m,
  111. valid.Field(&m.Name, valid.Required),
  112. valid.Field(&m.Level, valid.Required),
  113. )
  114. fmt.Println(err)
  115. // Output:
  116. // Level: cannot be blank; Name: cannot be blank.
  117. }
  118. type contextKey int
  119. func Example_six() {
  120. key := contextKey(1)
  121. rule := valid.WithContext(func(ctx context.Context, value interface{}) error {
  122. s, _ := value.(string)
  123. if ctx.Value(key) == s {
  124. return nil
  125. }
  126. return errors.New("unexpected value")
  127. })
  128. ctx := context.WithValue(context.Background(), key, "good sample")
  129. err1 := valid.ValidateWithContext(ctx, "bad sample", rule)
  130. fmt.Println(err1)
  131. err2 := valid.ValidateWithContext(ctx, "good sample", rule)
  132. fmt.Println(err2)
  133. // Output:
  134. // unexpected value
  135. // <nil>
  136. }
  137. func Example_seven() {
  138. c := map[string]interface{}{
  139. "Name": "Qiang Xue",
  140. "Email": "q",
  141. "Address": map[string]interface{}{
  142. "Street": "123",
  143. "City": "Unknown",
  144. "State": "Virginia",
  145. "Zip": "12345",
  146. },
  147. }
  148. err := valid.Validate(c,
  149. valid.Map(
  150. // Name cannot be empty, and the length must be between 5 and 20.
  151. valid.Key("Name", valid.Required, valid.Length(5, 20)),
  152. // Email cannot be empty and should be in a valid email format.
  153. valid.Key("Email", valid.Required, is.Email),
  154. // Validate Address using its own validation rules
  155. valid.Key("Address", valid.Map(
  156. // Street cannot be empty, and the length must between 5 and 50
  157. valid.Key("Street", valid.Required, valid.Length(5, 50)),
  158. // City cannot be empty, and the length must between 5 and 50
  159. valid.Key("City", valid.Required, valid.Length(5, 50)),
  160. // State cannot be empty, and must be a string consisting of two letters in upper case
  161. valid.Key("State", valid.Required, valid.Match(regexp.MustCompile("^[A-Z]{2}$"))),
  162. // State cannot be empty, and must be a string consisting of five digits
  163. valid.Key("Zip", valid.Required, valid.Match(regexp.MustCompile("^[0-9]{5}$"))),
  164. )),
  165. ),
  166. )
  167. fmt.Println(err)
  168. // Output:
  169. // Address: (State: must be in a valid format; Street: the length must be between 5 and 50.); Email: must be a valid email address.
  170. }