number.go 2.7 KB

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