gvalid_validator.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gvalid
  7. import (
  8. "context"
  9. "github.com/gogf/gf/i18n/gi18n"
  10. )
  11. // Validator is the validation manager for chaining operations.
  12. type Validator struct {
  13. ctx context.Context // Context containing custom context variables.
  14. i18nManager *gi18n.Manager // I18n manager for error message translation.
  15. key string // Single validation key.
  16. value interface{} // Single validation value.
  17. data interface{} // Validation data, which is usually a map.
  18. rules interface{} // Custom validation data.
  19. messages interface{} // Custom validation error messages, which can be string or type of CustomMsg.
  20. ruleFuncMap map[string]RuleFunc // ruleFuncMap stores custom rule functions for current Validator.
  21. useDataInsteadOfObjectAttributes bool // Using `data` as its validation source instead of attribute values from `Object`.
  22. bail bool // Stop validation after the first validation error.
  23. }
  24. // New creates and returns a new Validator.
  25. func New() *Validator {
  26. return &Validator{
  27. ctx: context.TODO(), // Initialize an empty context.
  28. i18nManager: gi18n.Instance(), // Use default i18n manager.
  29. ruleFuncMap: make(map[string]RuleFunc), // Custom rule function storing map.
  30. }
  31. }
  32. // Clone creates and returns a new Validator which is a shallow copy of current one.
  33. func (v *Validator) Clone() *Validator {
  34. newValidator := New()
  35. *newValidator = *v
  36. return newValidator
  37. }
  38. // I18n sets the i18n manager for the validator.
  39. func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator {
  40. newValidator := v.Clone()
  41. newValidator.i18nManager = i18nManager
  42. return newValidator
  43. }
  44. // Ctx is a chaining operation function, which sets the context for next validation.
  45. func (v *Validator) Ctx(ctx context.Context) *Validator {
  46. newValidator := v.Clone()
  47. newValidator.ctx = ctx
  48. return newValidator
  49. }
  50. // Bail sets the mark for stopping validation after the first validation error.
  51. func (v *Validator) Bail() *Validator {
  52. newValidator := v.Clone()
  53. newValidator.bail = true
  54. return newValidator
  55. }
  56. // Data is a chaining operation function, which sets validation data for current operation.
  57. // The parameter `data` usually be type of map, which specifies the parameter map used in validation.
  58. // Calling this function also sets `useDataInsteadOfObjectAttributes` true no mather the `data` is nil or not.
  59. func (v *Validator) Data(data interface{}) *Validator {
  60. newValidator := v.Clone()
  61. newValidator.data = data
  62. newValidator.useDataInsteadOfObjectAttributes = true
  63. return newValidator
  64. }
  65. // Rules is a chaining operation function, which sets custom validation rules for current operation.
  66. func (v *Validator) Rules(rules interface{}) *Validator {
  67. newValidator := v.Clone()
  68. newValidator.rules = rules
  69. return newValidator
  70. }
  71. // Messages is a chaining operation function, which sets custom error messages for current operation.
  72. // The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result
  73. // if `rules` is type of []string.
  74. func (v *Validator) Messages(messages interface{}) *Validator {
  75. newValidator := v.Clone()
  76. newValidator.messages = messages
  77. return newValidator
  78. }
  79. // RuleFunc registers one custom rule function to current Validator.
  80. func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator {
  81. newValidator := v.Clone()
  82. newValidator.ruleFuncMap[rule] = f
  83. return newValidator
  84. }
  85. // RuleFuncMap registers multiple custom rule functions to current Validator.
  86. func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator {
  87. newValidator := v.Clone()
  88. for k, v := range m {
  89. newValidator.ruleFuncMap[k] = v
  90. }
  91. return newValidator
  92. }
  93. // getRuleFunc retrieves and returns the custom rule function for specified rule.
  94. func (v *Validator) getRuleFunc(rule string) RuleFunc {
  95. ruleFunc := v.ruleFuncMap[rule]
  96. if ruleFunc == nil {
  97. ruleFunc = customRuleFuncMap[rule]
  98. }
  99. return ruleFunc
  100. }