| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package validator_test
- import (
- "context"
- "fmt"
- "testing"
- "git.ikuban.com/server/kratos-utils/v2/validator"
- // 引入你的 proto 包
- // pb "your-project/api/example"
- )
- // 示例 1: 基本使用
- func ExampleValidator_basic() {
- // 创建验证器
- _, err := validator.New()
- if err != nil {
- panic(err)
- }
- // 创建要验证的消息
- // msg := &pb.UserRequest{
- // Username: "test",
- // Email: "test@example.com",
- // }
- // 执行验证
- // err = v.Validate(ctx, msg)
- // if err != nil {
- // fmt.Println("验证失败:", err)
- // return
- // }
- fmt.Println("验证通过")
- // Output: 验证通过
- }
- // 示例 2: 使用验证组
- func ExampleValidator_withGroups() {
- ctx := context.Background()
- // 设置验证组
- ctx = validator.WithGroups(ctx, "create", "strict")
- // 创建消息
- // msg := &pb.UserRequest{...}
- // 使用默认验证器验证(会应用 create 和 strict 组的规则)
- // err := validator.Validate(ctx, msg)
- fmt.Println("使用验证组验证")
- }
- // 示例 3: 设置验证策略
- func ExampleValidator_withStrategy() {
- ctx := context.Background()
- // 创建自定义验证策略
- strategy := &validator.ValidationStrategy{
- Mode: validator.ValidationModeAll,
- FailFast: true, // 快速失败
- CollectWarnings: true, // 收集警告
- MaxErrors: 10, // 最多收集10个错误
- EnableCache: true, // 启用缓存
- }
- ctx = validator.WithStrategy(ctx, strategy)
- // 创建消息
- // msg := &pb.UserRequest{...}
- // 执行验证
- // err := validator.Validate(ctx, msg)
- fmt.Println("使用自定义策略验证")
- }
- // 示例 4: 设置语言
- func ExampleValidator_withLanguage() {
- ctx := context.Background()
- // 设置英文错误消息
- ctx = validator.WithLanguage(ctx, "en-US")
- // 创建消息
- // msg := &pb.UserRequest{...}
- // 执行验证(错误消息会使用英文)
- // err := validator.Validate(ctx, msg)
- fmt.Println("使用英文错误消息")
- }
- // 示例 5: 组合使用
- func ExampleValidator_combined() {
- ctx := context.Background()
- // 组合多个上下文设置
- ctx = validator.WithGroups(ctx, "create")
- ctx = validator.WithLanguage(ctx, "zh-CN")
- ctx = validator.WithAdmin(ctx, false)
- strategy := &validator.ValidationStrategy{
- FailFast: true,
- MaxErrors: 5,
- }
- ctx = validator.WithStrategy(ctx, strategy)
- // 创建消息
- // msg := &pb.UserRegistrationRequest{
- // Username: "testuser",
- // Password: "Test@123",
- // Mobile: "13800138000",
- // Email: "test@example.com",
- // }
- // 执行验证
- // err := validator.Validate(ctx, msg)
- // if err != nil {
- // fmt.Println("验证失败:", err)
- // return
- // }
- fmt.Println("组合验证完成")
- }
- // 测试: 中国手机号验证
- func TestChineseMobileValidation(t *testing.T) {
- testCases := []struct {
- mobile string
- valid bool
- }{
- {"13800138000", true}, // 移动
- {"18912345678", true}, // 电信
- {"15612345678", true}, // 联通
- {"12345678901", false}, // 无效前缀
- {"1380013800", false}, // 长度不足
- {"abc13800138000", false}, // 包含字母
- }
- for _, tc := range testCases {
- t.Run(tc.mobile, func(t *testing.T) {
- // 这里应该创建包含手机号字段的 proto 消息并验证
- // msg := &pb.UserRequest{Mobile: tc.mobile}
- // err := validator.Validate(context.Background(), msg)
- //
- // if tc.valid && err != nil {
- // t.Errorf("期望验证通过,但失败了: %v", err)
- // }
- // if !tc.valid && err == nil {
- // t.Error("期望验证失败,但通过了")
- // }
- })
- }
- }
- // 测试: 中国身份证验证
- func TestChineseIDCardValidation(t *testing.T) {
- testCases := []struct {
- idCard string
- valid bool
- }{
- {"110101199003074218", true}, // 有效的18位身份证
- {"320301199001011234", false}, // 校验位错误
- {"12345678901234567", false}, // 格式错误
- }
- for _, tc := range testCases {
- t.Run(tc.idCard, func(t *testing.T) {
- // 实际验证逻辑
- })
- }
- }
- // 基准测试: 验证性能
- func BenchmarkValidator(b *testing.B) {
- v, _ := validator.New()
- ctx := context.Background()
- // msg := &pb.UserRequest{
- // Username: "testuser",
- // Email: "test@example.com",
- // Mobile: "13800138000",
- // }
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- // _ = v.Validate(ctx, msg)
- _ = ctx
- _ = v
- }
- }
- // 基准测试: CEL 表达式性能
- func BenchmarkCELValidation(b *testing.B) {
- v, _ := validator.New()
- ctx := context.Background()
- // 包含 CEL 规则的消息
- // msg := &pb.CELExample{
- // Password: "Test@12345",
- // }
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- // _ = v.Validate(ctx, msg)
- _ = ctx
- _ = v
- }
- }
|