example_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package validator_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "git.ikuban.com/server/kratos-utils/v2/validator"
  7. // 引入你的 proto 包
  8. // pb "your-project/api/example"
  9. )
  10. // 示例 1: 基本使用
  11. func ExampleValidator_basic() {
  12. // 创建验证器
  13. _, err := validator.New()
  14. if err != nil {
  15. panic(err)
  16. }
  17. // 创建要验证的消息
  18. // msg := &pb.UserRequest{
  19. // Username: "test",
  20. // Email: "test@example.com",
  21. // }
  22. // 执行验证
  23. // err = v.Validate(ctx, msg)
  24. // if err != nil {
  25. // fmt.Println("验证失败:", err)
  26. // return
  27. // }
  28. fmt.Println("验证通过")
  29. // Output: 验证通过
  30. }
  31. // 示例 2: 使用验证组
  32. func ExampleValidator_withGroups() {
  33. ctx := context.Background()
  34. // 设置验证组
  35. ctx = validator.WithGroups(ctx, "create", "strict")
  36. // 创建消息
  37. // msg := &pb.UserRequest{...}
  38. // 使用默认验证器验证(会应用 create 和 strict 组的规则)
  39. // err := validator.Validate(ctx, msg)
  40. fmt.Println("使用验证组验证")
  41. }
  42. // 示例 3: 设置验证策略
  43. func ExampleValidator_withStrategy() {
  44. ctx := context.Background()
  45. // 创建自定义验证策略
  46. strategy := &validator.ValidationStrategy{
  47. Mode: validator.ValidationModeAll,
  48. FailFast: true, // 快速失败
  49. CollectWarnings: true, // 收集警告
  50. MaxErrors: 10, // 最多收集10个错误
  51. EnableCache: true, // 启用缓存
  52. }
  53. ctx = validator.WithStrategy(ctx, strategy)
  54. // 创建消息
  55. // msg := &pb.UserRequest{...}
  56. // 执行验证
  57. // err := validator.Validate(ctx, msg)
  58. fmt.Println("使用自定义策略验证")
  59. }
  60. // 示例 4: 设置语言
  61. func ExampleValidator_withLanguage() {
  62. ctx := context.Background()
  63. // 设置英文错误消息
  64. ctx = validator.WithLanguage(ctx, "en-US")
  65. // 创建消息
  66. // msg := &pb.UserRequest{...}
  67. // 执行验证(错误消息会使用英文)
  68. // err := validator.Validate(ctx, msg)
  69. fmt.Println("使用英文错误消息")
  70. }
  71. // 示例 5: 组合使用
  72. func ExampleValidator_combined() {
  73. ctx := context.Background()
  74. // 组合多个上下文设置
  75. ctx = validator.WithGroups(ctx, "create")
  76. ctx = validator.WithLanguage(ctx, "zh-CN")
  77. ctx = validator.WithAdmin(ctx, false)
  78. strategy := &validator.ValidationStrategy{
  79. FailFast: true,
  80. MaxErrors: 5,
  81. }
  82. ctx = validator.WithStrategy(ctx, strategy)
  83. // 创建消息
  84. // msg := &pb.UserRegistrationRequest{
  85. // Username: "testuser",
  86. // Password: "Test@123",
  87. // Mobile: "13800138000",
  88. // Email: "test@example.com",
  89. // }
  90. // 执行验证
  91. // err := validator.Validate(ctx, msg)
  92. // if err != nil {
  93. // fmt.Println("验证失败:", err)
  94. // return
  95. // }
  96. fmt.Println("组合验证完成")
  97. }
  98. // 测试: 中国手机号验证
  99. func TestChineseMobileValidation(t *testing.T) {
  100. testCases := []struct {
  101. mobile string
  102. valid bool
  103. }{
  104. {"13800138000", true}, // 移动
  105. {"18912345678", true}, // 电信
  106. {"15612345678", true}, // 联通
  107. {"12345678901", false}, // 无效前缀
  108. {"1380013800", false}, // 长度不足
  109. {"abc13800138000", false}, // 包含字母
  110. }
  111. for _, tc := range testCases {
  112. t.Run(tc.mobile, func(t *testing.T) {
  113. // 这里应该创建包含手机号字段的 proto 消息并验证
  114. // msg := &pb.UserRequest{Mobile: tc.mobile}
  115. // err := validator.Validate(context.Background(), msg)
  116. //
  117. // if tc.valid && err != nil {
  118. // t.Errorf("期望验证通过,但失败了: %v", err)
  119. // }
  120. // if !tc.valid && err == nil {
  121. // t.Error("期望验证失败,但通过了")
  122. // }
  123. })
  124. }
  125. }
  126. // 测试: 中国身份证验证
  127. func TestChineseIDCardValidation(t *testing.T) {
  128. testCases := []struct {
  129. idCard string
  130. valid bool
  131. }{
  132. {"110101199003074218", true}, // 有效的18位身份证
  133. {"320301199001011234", false}, // 校验位错误
  134. {"12345678901234567", false}, // 格式错误
  135. }
  136. for _, tc := range testCases {
  137. t.Run(tc.idCard, func(t *testing.T) {
  138. // 实际验证逻辑
  139. })
  140. }
  141. }
  142. // 基准测试: 验证性能
  143. func BenchmarkValidator(b *testing.B) {
  144. v, _ := validator.New()
  145. ctx := context.Background()
  146. // msg := &pb.UserRequest{
  147. // Username: "testuser",
  148. // Email: "test@example.com",
  149. // Mobile: "13800138000",
  150. // }
  151. b.ResetTimer()
  152. for i := 0; i < b.N; i++ {
  153. // _ = v.Validate(ctx, msg)
  154. _ = ctx
  155. _ = v
  156. }
  157. }
  158. // 基准测试: CEL 表达式性能
  159. func BenchmarkCELValidation(b *testing.B) {
  160. v, _ := validator.New()
  161. ctx := context.Background()
  162. // 包含 CEL 规则的消息
  163. // msg := &pb.CELExample{
  164. // Password: "Test@12345",
  165. // }
  166. b.ResetTimer()
  167. for i := 0; i < b.N; i++ {
  168. // _ = v.Validate(ctx, msg)
  169. _ = ctx
  170. _ = v
  171. }
  172. }