validators.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package form
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. )
  8. // validation errors
  9. var (
  10. EInvalidInteger = errors.New("not a valid integer value")
  11. EInvalidFloat = errors.New("not a valid float value")
  12. ERequired = errors.New("this field is required")
  13. )
  14. // validLettersGeneric is a validator generator for checking
  15. // for valid letters in field
  16. func validLettersGeneric(Letters string, Error error) ValidatorFunc {
  17. Callback := func(field FormField, ctx context.Context) error {
  18. for _, Rune := range field.GetString() {
  19. if strings.IndexRune(Letters, Rune) == -1 {
  20. return Error
  21. }
  22. }
  23. return nil
  24. }
  25. return Callback
  26. }
  27. // validRequired returns nil if there is text in the field.
  28. // If the field is empty it returns error.
  29. func validRequired(field FormField, ctx context.Context) error {
  30. if field.GetString() == "" {
  31. return ERequired
  32. }
  33. return nil
  34. }
  35. // validLength returns a string field validator that verifies if a string
  36. // length is between specified min and max values.
  37. func validLength(min, max int) ValidatorFunc {
  38. var ELength = errors.New(
  39. fmt.Sprintf("must be a string between %d and %d characters in length", min, max))
  40. return func(field FormField, ctx context.Context) error {
  41. if len(field.GetString()) != 0 && (len(field.GetString()) < min || len(field.GetString()) > max) {
  42. return ELength
  43. }
  44. return nil
  45. }
  46. }
  47. // validInt returns error if field does not contain a valid integer value
  48. func validInt(field FormField, ctx context.Context) error {
  49. _, err := field.GetInt()
  50. if err != nil {
  51. return EInvalidInteger
  52. }
  53. return nil
  54. }
  55. // validBetween is a validator generator that makes sure field is
  56. // integer value within the specified range.
  57. func validBetween(min, max int) ValidatorFunc {
  58. var EInvalidInterval = errors.New(
  59. fmt.Sprintf("must be integer between %d and %d", min, max))
  60. return func(field FormField, ctx context.Context) error {
  61. value, err := field.GetInt()
  62. if err != nil {
  63. return err
  64. }
  65. if value < min || value > max {
  66. return EInvalidInterval
  67. }
  68. return nil
  69. }
  70. }
  71. // validFloat returns error if field does not contain a valid integer value
  72. func validFloat(field FormField, ctx context.Context) error {
  73. _, err := field.GetFloat()
  74. if err != nil {
  75. return EInvalidFloat
  76. }
  77. return nil
  78. }
  79. // validBetweenFloat32 is a v alidator generator that makes sure field is
  80. // float64 value within the specified range.
  81. func validBetweenFloat(min, max float64) ValidatorFunc {
  82. var EInvalidInterval = errors.New(
  83. fmt.Sprintf("must be float value between %.2f and %.2f", min, max))
  84. return func(field FormField, ctx context.Context) error {
  85. value, err := field.GetFloat()
  86. if err != nil {
  87. return err
  88. }
  89. if value < min || value > max {
  90. return EInvalidInterval
  91. }
  92. return nil
  93. }
  94. }
  95. // validFieldEqualTo is a general purpose validator that checks
  96. // if two fields have the same value for verification purposes.
  97. func validFieldEqualTo(Other *FormField, err error) ValidatorFunc {
  98. return func(field FormField, ctx context.Context) error {
  99. if field.GetString() != Other.GetString() {
  100. return err
  101. }
  102. return nil
  103. }
  104. }