number.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package validate
  2. // Validation error.
  3. var (
  4. ErrMustBeGreater = NewError("must be greater than %v")
  5. ErrMustBeGreaterOrEqual = NewError("must be greater than or equal to %v")
  6. ErrMustBeLess = NewError("must be less than %v")
  7. ErrMustBeLessOrEqual = NewError("must be less than or equal to %v")
  8. )
  9. // Max validates whether an integer is less than or equal to a given maximum.
  10. // If exclusive is true, an equal value will also produce an error.
  11. func Max(n int, exclusive bool) func(int) error {
  12. return func(value int) error {
  13. if exclusive {
  14. if value >= n {
  15. return ErrMustBeLess.With(n)
  16. }
  17. }
  18. if value > n {
  19. return ErrMustBeLessOrEqual.With(n)
  20. }
  21. return nil
  22. }
  23. }
  24. // MaxFloat32 validates whether a float32 is less than or equal to a given maximum.
  25. // If exclusive is true, an equal value will also produce an error.
  26. func MaxFloat32(n float32, exclusive bool) func(float32) error {
  27. return func(value float32) error {
  28. if exclusive {
  29. if value >= n {
  30. return ErrMustBeLess.With(n)
  31. }
  32. }
  33. if value > n {
  34. return ErrMustBeLessOrEqual.With(n)
  35. }
  36. return nil
  37. }
  38. }
  39. // MaxFloat64 validates whether a float64 is less than or equal to a given maximum.
  40. // If exclusive is true, an equal value will also produce an error.
  41. func MaxFloat64(n float64, exclusive bool) func(float64) error {
  42. return func(value float64) error {
  43. if exclusive {
  44. if value >= n {
  45. return ErrMustBeLess.With(n)
  46. }
  47. }
  48. if value > n {
  49. return ErrMustBeLessOrEqual.With(n)
  50. }
  51. return nil
  52. }
  53. }
  54. // Min validates whether an integer is less than or equal to a given maximum.
  55. // If exclusive is true, an equal value will also produce an error.
  56. func Min(n int, exclusive bool) func(int) error {
  57. return func(value int) error {
  58. if exclusive {
  59. if value <= n {
  60. return ErrMustBeGreater.With(n)
  61. }
  62. }
  63. if value < n {
  64. return ErrMustBeGreaterOrEqual.With(n)
  65. }
  66. return nil
  67. }
  68. }
  69. // MinFloat32 validates whether a float32 is less than or equal to a given maximum.
  70. // If exclusive is true, an equal value will also produce an error.
  71. func MinFloat32(n float32, exclusive bool) func(float32) error {
  72. return func(value float32) error {
  73. if exclusive {
  74. if value <= n {
  75. return ErrMustBeGreater.With(n)
  76. }
  77. }
  78. if value < n {
  79. return ErrMustBeGreaterOrEqual.With(n)
  80. }
  81. return nil
  82. }
  83. }
  84. // MinFloat64 validates whether a float64 is less than or equal to a given maximum.
  85. // If exclusive is true, an equal value will also produce an error.
  86. func MinFloat64(n float64, exclusive bool) func(float64) error {
  87. return func(value float64) error {
  88. if exclusive {
  89. if value <= n {
  90. return ErrMustBeGreater.With(n)
  91. }
  92. }
  93. if value < n {
  94. return ErrMustBeGreaterOrEqual.With(n)
  95. }
  96. return nil
  97. }
  98. }