validator.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package validator
  2. import (
  3. "context"
  4. "google.golang.org/protobuf/proto"
  5. )
  6. // Validator 验证器接口
  7. type Validator interface {
  8. // Validate 验证消息
  9. Validate(ctx context.Context, msg proto.Message) error
  10. // ValidateWithGroups 使用指定组验证消息
  11. ValidateWithGroups(ctx context.Context, msg proto.Message, groups ...string) error
  12. // ValidateField 验证单个字段
  13. ValidateField(ctx context.Context, fieldPath string, value interface{}, rules interface{}) error
  14. }
  15. // validator 默认验证器实现
  16. type validator struct {
  17. engine *Engine
  18. celExecutor *CELExecutor
  19. }
  20. // New 创建新的验证器
  21. func New(opts ...Option) (Validator, error) {
  22. // 创建 CEL 执行器
  23. celExecutor, err := NewCELExecutor()
  24. if err != nil {
  25. return nil, err
  26. }
  27. // 创建验证引擎
  28. engine := NewEngine(celExecutor)
  29. v := &validator{
  30. engine: engine,
  31. celExecutor: celExecutor,
  32. }
  33. // 应用选项
  34. for _, opt := range opts {
  35. opt(v)
  36. }
  37. return v, nil
  38. }
  39. // Validate 验证消息
  40. func (v *validator) Validate(ctx context.Context, msg proto.Message) error {
  41. if msg == nil {
  42. return nil
  43. }
  44. // 从上下文创建验证上下文
  45. vctx := FromContext(ctx)
  46. // 执行验证
  47. return v.engine.Validate(vctx, msg)
  48. }
  49. // ValidateWithGroups 使用指定组验证消息
  50. func (v *validator) ValidateWithGroups(ctx context.Context, msg proto.Message, groups ...string) error {
  51. if msg == nil {
  52. return nil
  53. }
  54. // 创建验证上下文并设置组
  55. vctx := FromContext(ctx)
  56. vctx.Groups = groups
  57. // 执行验证
  58. return v.engine.Validate(vctx, msg)
  59. }
  60. // ValidateField 验证单个字段
  61. func (v *validator) ValidateField(ctx context.Context, fieldPath string, value interface{}, rules interface{}) error {
  62. vctx := FromContext(ctx)
  63. return v.engine.ValidateField(vctx, fieldPath, value, rules)
  64. }
  65. // Option 验证器选项
  66. type Option func(*validator)
  67. // WithCELFunctions 添加自定义 CEL 函数(暂时保留接口,未来扩展)
  68. func WithCELFunctions() Option {
  69. return func(v *validator) {
  70. // TODO: 实现自定义 CEL 函数注册
  71. }
  72. }
  73. // MustNew 创建验证器,如果失败则 panic
  74. func MustNew(opts ...Option) Validator {
  75. v, err := New(opts...)
  76. if err != nil {
  77. panic(err)
  78. }
  79. return v
  80. }
  81. // 全局默认验证器
  82. var defaultValidator Validator
  83. // init 初始化默认验证器
  84. func init() {
  85. var err error
  86. defaultValidator, err = New()
  87. if err != nil {
  88. panic(err)
  89. }
  90. }
  91. // Validate 使用默认验证器验证消息
  92. func Validate(ctx context.Context, msg proto.Message) error {
  93. return defaultValidator.Validate(ctx, msg)
  94. }
  95. // ValidateWithGroups 使用默认验证器和指定组验证消息
  96. func ValidateWithGroups(ctx context.Context, msg proto.Message, groups ...string) error {
  97. return defaultValidator.ValidateWithGroups(ctx, msg, groups...)
  98. }