validators.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // ValidFieldIn verifies if item is within the list of items
  48. func ValidFieldIn(list []string) ValidatorFunc {
  49. var EInvalidValue = errors.New(
  50. fmt.Sprintf(
  51. "field value must be one of: %s",
  52. strings.Join(list, ","),
  53. ),
  54. )
  55. return func(field FormField, ctx context.Context) error {
  56. for _, item := range list {
  57. if item == field.GetString() {
  58. return nil
  59. }
  60. }
  61. return EInvalidValue
  62. }
  63. }
  64. // ValidInt returns error if field does not contain a valid integer value
  65. func ValidInt(field FormField, ctx context.Context) error {
  66. _, err := field.GetInt()
  67. if err != nil {
  68. return EInvalidInteger
  69. }
  70. return nil
  71. }
  72. // ValidBetween is a validator generator that makes sure field is
  73. // integer value within the specified range.
  74. func ValidBetween(min, max int) ValidatorFunc {
  75. var EInvalidInterval = errors.New(
  76. fmt.Sprintf("must be integer between %d and %d", min, max))
  77. return func(field FormField, ctx context.Context) error {
  78. value, err := field.GetInt()
  79. if err != nil {
  80. return err
  81. }
  82. if value < min || value > max {
  83. return EInvalidInterval
  84. }
  85. return nil
  86. }
  87. }
  88. // ValidFloat returns error if field does not contain a valid integer value
  89. func ValidFloat(field FormField, ctx context.Context) error {
  90. _, err := field.GetFloat()
  91. if err != nil {
  92. return EInvalidFloat
  93. }
  94. return nil
  95. }
  96. // ValidBetweenFloat32 is a v alidator generator that makes sure field is
  97. // float64 value within the specified range.
  98. func ValidBetweenFloat(min, max float64) ValidatorFunc {
  99. var EInvalidInterval = errors.New(
  100. fmt.Sprintf("must be float value between %.2f and %.2f", min, max))
  101. return func(field FormField, ctx context.Context) error {
  102. value, err := field.GetFloat()
  103. if err != nil {
  104. return err
  105. }
  106. if value < min || value > max {
  107. return EInvalidInterval
  108. }
  109. return nil
  110. }
  111. }
  112. // ValidFieldEqualTo is a general purpose validator that checks
  113. // if two fields have the same value for verification purposes.
  114. func ValidFieldEqualTo(Other *FormField, err error) ValidatorFunc {
  115. return func(field FormField, ctx context.Context) error {
  116. if field.GetString() != Other.GetString() {
  117. return err
  118. }
  119. return nil
  120. }
  121. }