context.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package validator
  2. import (
  3. "context"
  4. )
  5. // contextKey 上下文键类型
  6. type contextKey string
  7. const (
  8. // 验证组
  9. contextKeyGroups contextKey = "validator:groups"
  10. // 验证策略
  11. contextKeyStrategy contextKey = "validator:strategy"
  12. // 语言
  13. contextKeyLanguage contextKey = "validator:language"
  14. // 用户角色
  15. contextKeyRoles contextKey = "validator:roles"
  16. // 是否管理员
  17. contextKeyIsAdmin contextKey = "validator:is_admin"
  18. )
  19. // ValidationContext 验证上下文
  20. type ValidationContext struct {
  21. // 激活的验证组
  22. Groups []string
  23. // 验证策略
  24. Strategy *ValidationStrategy
  25. // 语言偏好
  26. Language string
  27. // 用户角色
  28. Roles []string
  29. // 是否管理员
  30. IsAdmin bool
  31. // 自定义元数据
  32. Metadata map[string]interface{}
  33. }
  34. // ValidationStrategy 验证策略
  35. type ValidationStrategy struct {
  36. // 验证模式
  37. Mode ValidationMode
  38. // 是否快速失败(遇到第一个错误就停止)
  39. FailFast bool
  40. // 是否收集警告
  41. CollectWarnings bool
  42. // 最大错误数(超过后停止验证)
  43. MaxErrors int
  44. // 是否启用缓存
  45. EnableCache bool
  46. }
  47. // ValidationMode 验证模式
  48. type ValidationMode int
  49. const (
  50. // 验证所有规则
  51. ValidationModeAll ValidationMode = iota
  52. // 仅验证必填项
  53. ValidationModeRequiredOnly
  54. // 仅验证修改的字段
  55. ValidationModeChangedOnly
  56. // 自定义(根据组选择)
  57. ValidationModeCustom
  58. )
  59. // NewContext 创建验证上下文
  60. func NewContext() *ValidationContext {
  61. return &ValidationContext{
  62. Groups: make([]string, 0),
  63. Metadata: make(map[string]interface{}),
  64. Strategy: DefaultStrategy(),
  65. }
  66. }
  67. // DefaultStrategy 默认验证策略
  68. func DefaultStrategy() *ValidationStrategy {
  69. return &ValidationStrategy{
  70. Mode: ValidationModeAll,
  71. FailFast: false,
  72. CollectWarnings: true,
  73. MaxErrors: 100,
  74. EnableCache: true,
  75. }
  76. }
  77. // WithGroups 设置验证组到上下文
  78. func WithGroups(ctx context.Context, groups ...string) context.Context {
  79. return context.WithValue(ctx, contextKeyGroups, groups)
  80. }
  81. // GetGroups 从上下文获取验证组
  82. func GetGroups(ctx context.Context) []string {
  83. if groups, ok := ctx.Value(contextKeyGroups).([]string); ok {
  84. return groups
  85. }
  86. return nil
  87. }
  88. // WithStrategy 设置验证策略到上下文
  89. func WithStrategy(ctx context.Context, strategy *ValidationStrategy) context.Context {
  90. return context.WithValue(ctx, contextKeyStrategy, strategy)
  91. }
  92. // GetStrategy 从上下文获取验证策略
  93. func GetStrategy(ctx context.Context) *ValidationStrategy {
  94. if strategy, ok := ctx.Value(contextKeyStrategy).(*ValidationStrategy); ok {
  95. return strategy
  96. }
  97. return DefaultStrategy()
  98. }
  99. // WithLanguage 设置语言到上下文
  100. func WithLanguage(ctx context.Context, lang string) context.Context {
  101. return context.WithValue(ctx, contextKeyLanguage, lang)
  102. }
  103. // GetLanguage 从上下文获取语言
  104. func GetLanguage(ctx context.Context) string {
  105. if lang, ok := ctx.Value(contextKeyLanguage).(string); ok {
  106. return lang
  107. }
  108. return "zh-CN" // 默认中文
  109. }
  110. // WithRoles 设置用户角色到上下文
  111. func WithRoles(ctx context.Context, roles ...string) context.Context {
  112. return context.WithValue(ctx, contextKeyRoles, roles)
  113. }
  114. // GetRoles 从上下文获取用户角色
  115. func GetRoles(ctx context.Context) []string {
  116. if roles, ok := ctx.Value(contextKeyRoles).([]string); ok {
  117. return roles
  118. }
  119. return nil
  120. }
  121. // WithAdmin 设置管理员标志到上下文
  122. func WithAdmin(ctx context.Context, isAdmin bool) context.Context {
  123. return context.WithValue(ctx, contextKeyIsAdmin, isAdmin)
  124. }
  125. // IsAdmin 从上下文判断是否管理员
  126. func IsAdmin(ctx context.Context) bool {
  127. if isAdmin, ok := ctx.Value(contextKeyIsAdmin).(bool); ok {
  128. return isAdmin
  129. }
  130. return false
  131. }
  132. // FromContext 从上下文创建 ValidationContext
  133. func FromContext(ctx context.Context) *ValidationContext {
  134. vc := NewContext()
  135. vc.Groups = GetGroups(ctx)
  136. vc.Strategy = GetStrategy(ctx)
  137. vc.Language = GetLanguage(ctx)
  138. vc.Roles = GetRoles(ctx)
  139. vc.IsAdmin = IsAdmin(ctx)
  140. return vc
  141. }
  142. // HasGroup 检查是否包含指定组
  143. func (vc *ValidationContext) HasGroup(group string) bool {
  144. if len(vc.Groups) == 0 {
  145. // 没有指定组时,默认验证所有规则
  146. return true
  147. }
  148. for _, g := range vc.Groups {
  149. if g == group {
  150. return true
  151. }
  152. }
  153. return false
  154. }
  155. // HasAnyGroup 检查是否包含任一指定组
  156. func (vc *ValidationContext) HasAnyGroup(groups []string) bool {
  157. if len(vc.Groups) == 0 {
  158. return true
  159. }
  160. if len(groups) == 0 {
  161. return true
  162. }
  163. for _, g := range groups {
  164. if vc.HasGroup(g) {
  165. return true
  166. }
  167. }
  168. return false
  169. }
  170. // ShouldFailFast 是否应该快速失败
  171. func (vc *ValidationContext) ShouldFailFast() bool {
  172. return vc.Strategy != nil && vc.Strategy.FailFast
  173. }
  174. // ShouldCollectWarnings 是否应该收集警告
  175. func (vc *ValidationContext) ShouldCollectWarnings() bool {
  176. return vc.Strategy == nil || vc.Strategy.CollectWarnings
  177. }
  178. // MaxErrorsReached 是否达到最大错误数
  179. func (vc *ValidationContext) MaxErrorsReached(errorCount int) bool {
  180. if vc.Strategy == nil {
  181. return false
  182. }
  183. return vc.Strategy.MaxErrors > 0 && errorCount >= vc.Strategy.MaxErrors
  184. }