minmax.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2016 Qiang Xue. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "time"
  10. )
  11. type ThresholdRule struct {
  12. threshold interface{}
  13. operator int
  14. message string
  15. }
  16. const (
  17. greaterThan = iota
  18. greaterEqualThan
  19. lessThan
  20. lessEqualThan
  21. )
  22. // Min is a validation rule that checks if a value is greater or equal than the specified value.
  23. // By calling Exclusive, the rule will check if the value is strictly greater than the specified value.
  24. // Note that the value being checked and the threshold value must be of the same type.
  25. // Only int, uint, float and time.Time types are supported.
  26. // An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
  27. func Min(min interface{}) *ThresholdRule {
  28. return &ThresholdRule{
  29. threshold: min,
  30. operator: greaterEqualThan,
  31. message: fmt.Sprintf("must be no less than %v", min),
  32. }
  33. }
  34. // Max is a validation rule that checks if a value is less or equal than the specified value.
  35. // By calling Exclusive, the rule will check if the value is strictly less than the specified value.
  36. // Note that the value being checked and the threshold value must be of the same type.
  37. // Only int, uint, float and time.Time types are supported.
  38. // An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
  39. func Max(max interface{}) *ThresholdRule {
  40. return &ThresholdRule{
  41. threshold: max,
  42. operator: lessEqualThan,
  43. message: fmt.Sprintf("must be no greater than %v", max),
  44. }
  45. }
  46. // Exclusive sets the comparison to exclude the boundary value.
  47. func (r *ThresholdRule) Exclusive() *ThresholdRule {
  48. if r.operator == greaterEqualThan {
  49. r.operator = greaterThan
  50. r.message = fmt.Sprintf("must be greater than %v", r.threshold)
  51. } else if r.operator == lessEqualThan {
  52. r.operator = lessThan
  53. r.message = fmt.Sprintf("must be less than %v", r.threshold)
  54. }
  55. return r
  56. }
  57. // Validate checks if the given value is valid or not.
  58. func (r *ThresholdRule) Validate(value interface{}) error {
  59. value, isNil := Indirect(value)
  60. if isNil || IsEmpty(value) {
  61. return nil
  62. }
  63. rv := reflect.ValueOf(r.threshold)
  64. switch rv.Kind() {
  65. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  66. v, err := ToInt(value)
  67. if err != nil {
  68. return err
  69. }
  70. if r.compareInt(rv.Int(), v) {
  71. return nil
  72. }
  73. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  74. v, err := ToUint(value)
  75. if err != nil {
  76. return err
  77. }
  78. if r.compareUint(rv.Uint(), v) {
  79. return nil
  80. }
  81. case reflect.Float32, reflect.Float64:
  82. v, err := ToFloat(value)
  83. if err != nil {
  84. return err
  85. }
  86. if r.compareFloat(rv.Float(), v) {
  87. return nil
  88. }
  89. case reflect.Struct:
  90. t, ok := r.threshold.(time.Time)
  91. if !ok {
  92. return fmt.Errorf("type not supported: %v", rv.Type())
  93. }
  94. v, ok := value.(time.Time)
  95. if !ok {
  96. return fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value))
  97. }
  98. if v.IsZero() || r.compareTime(t, v) {
  99. return nil
  100. }
  101. default:
  102. return fmt.Errorf("type not supported: %v", rv.Type())
  103. }
  104. return errors.New(r.message)
  105. }
  106. // Error sets the error message for the rule.
  107. func (r *ThresholdRule) Error(message string) *ThresholdRule {
  108. r.message = message
  109. return r
  110. }
  111. func (r *ThresholdRule) compareInt(threshold, value int64) bool {
  112. switch r.operator {
  113. case greaterThan:
  114. return value > threshold
  115. case greaterEqualThan:
  116. return value >= threshold
  117. case lessThan:
  118. return value < threshold
  119. default:
  120. return value <= threshold
  121. }
  122. }
  123. func (r *ThresholdRule) compareUint(threshold, value uint64) bool {
  124. switch r.operator {
  125. case greaterThan:
  126. return value > threshold
  127. case greaterEqualThan:
  128. return value >= threshold
  129. case lessThan:
  130. return value < threshold
  131. default:
  132. return value <= threshold
  133. }
  134. }
  135. func (r *ThresholdRule) compareFloat(threshold, value float64) bool {
  136. switch r.operator {
  137. case greaterThan:
  138. return value > threshold
  139. case greaterEqualThan:
  140. return value >= threshold
  141. case lessThan:
  142. return value < threshold
  143. default:
  144. return value <= threshold
  145. }
  146. }
  147. func (r *ThresholdRule) compareTime(threshold, value time.Time) bool {
  148. switch r.operator {
  149. case greaterThan:
  150. return value.After(threshold)
  151. case greaterEqualThan:
  152. return value.After(threshold) || value.Equal(threshold)
  153. case lessThan:
  154. return value.Before(threshold)
  155. default:
  156. return value.Before(threshold) || value.Equal(threshold)
  157. }
  158. }