rule_length.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package valid
  2. import (
  3. "unicode/utf8"
  4. )
  5. var (
  6. // ErrLengthTooLong is the error that returns in case of too long length.
  7. ErrLengthTooLong = NewError("validation_length_too_long", "the length must be no more than {{.max}}")
  8. // ErrLengthTooShort is the error that returns in case of too short length.
  9. ErrLengthTooShort = NewError("validation_length_too_short", "the length must be no less than {{.min}}")
  10. // ErrLengthInvalid is the error that returns in case of an invalid length.
  11. ErrLengthInvalid = NewError("validation_length_invalid", "the length must be exactly {{.min}}")
  12. // ErrLengthOutOfRange is the error that returns in case of out of range length.
  13. ErrLengthOutOfRange = NewError("validation_length_out_of_range", "the length must be between {{.min}} and {{.max}}")
  14. // ErrLengthEmptyRequired is the error that returns in case of non-empty value.
  15. ErrLengthEmptyRequired = NewError("validation_length_empty_required", "the value must be empty")
  16. )
  17. // Length returns a validation rule that checks if a value's length is within the specified range.
  18. // If max is 0, it means there is no upper bound for the length.
  19. // This rule should only be used for validating strings, slices, maps, and arrays.
  20. // An empty value is considered valid. Use the Required rule to make sure a value is not empty.
  21. func Length(min, max int) LengthRule {
  22. return LengthRule{min: min, max: max, err: buildLengthRuleError(min, max)}
  23. }
  24. // RuneLength returns a validation rule that checks if a string's rune length is within the specified range.
  25. // If max is 0, it means there is no upper bound for the length.
  26. // This rule should only be used for validating strings, slices, maps, and arrays.
  27. // An empty value is considered valid. Use the Required rule to make sure a value is not empty.
  28. // If the value being validated is not a string, the rule works the same as Length.
  29. func RuneLength(min, max int) LengthRule {
  30. r := Length(min, max)
  31. r.rune = true
  32. return r
  33. }
  34. // LengthRule is a validation rule that checks if a value's length is within the specified range.
  35. type LengthRule struct {
  36. err Error
  37. min, max int
  38. rune bool
  39. }
  40. // Validate checks if the given value is valid or not.
  41. func (r LengthRule) Validate(value interface{}) error {
  42. value, isNil := Indirect(value)
  43. if isNil || IsEmpty(value) {
  44. return nil
  45. }
  46. var (
  47. l int
  48. err error
  49. )
  50. if s, ok := value.(string); ok && r.rune {
  51. l = utf8.RuneCountInString(s)
  52. } else if l, err = LengthOfValue(value); err != nil {
  53. return err
  54. }
  55. if r.min > 0 && l < r.min || r.max > 0 && l > r.max || r.min == 0 && r.max == 0 && l > 0 {
  56. return r.err
  57. }
  58. return nil
  59. }
  60. // Error sets the error message for the rule.
  61. func (r LengthRule) Error(message string) LengthRule {
  62. r.err = r.err.SetMessage(message)
  63. return r
  64. }
  65. // ErrorObject sets the error struct for the rule.
  66. func (r LengthRule) ErrorObject(err Error) LengthRule {
  67. r.err = err
  68. return r
  69. }
  70. func buildLengthRuleError(min, max int) (err Error) {
  71. if min == 0 && max > 0 {
  72. err = ErrLengthTooLong
  73. } else if min > 0 && max == 0 {
  74. err = ErrLengthTooShort
  75. } else if min > 0 && max > 0 {
  76. if min == max {
  77. err = ErrLengthInvalid
  78. } else {
  79. err = ErrLengthOutOfRange
  80. }
  81. } else {
  82. err = ErrLengthEmptyRequired
  83. }
  84. return err.SetParams(map[string]interface{}{"min": min, "max": max})
  85. }