validators.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 for valid letters in field */
  15. func ValidLettersGeneric(Letters string, Error error) ValidatorFunc {
  16. Callback := func(field FormField, ctx context.Context) error {
  17. for _, Rune := range field.GetString() {
  18. if strings.IndexRune(Letters, Rune) == -1 {
  19. return Error
  20. }
  21. }
  22. return nil
  23. }
  24. return Callback
  25. }
  26. /* ValidRequired makes sure field is not empty. */
  27. func ValidRequired(field FormField, ctx context.Context) error {
  28. if field.GetString() == "" {
  29. return ERequired
  30. }
  31. return nil
  32. }
  33. /* ValidLength makes sure that a string length is between specified min and max values. */
  34. func ValidLength(min, max int) ValidatorFunc {
  35. var ELength = errors.New(
  36. fmt.Sprintf("must be a string between %d and %d characters in length", min, max))
  37. return func(field FormField, ctx context.Context) error {
  38. if len(field.GetString()) != 0 && (len(field.GetString()) < min || len(field.GetString()) > max) {
  39. return ELength
  40. }
  41. return nil
  42. }
  43. }
  44. /* ValidFieldIn verifies if item is within the list of items */
  45. func ValidFieldIn(list []string) ValidatorFunc {
  46. var EInvalidValue = errors.New(
  47. fmt.Sprintf(
  48. "field value must be one of: %s",
  49. strings.Join(list, ","),
  50. ),
  51. )
  52. return func(field FormField, ctx context.Context) error {
  53. for _, item := range list {
  54. if item == field.GetString() {
  55. return nil
  56. }
  57. }
  58. return EInvalidValue
  59. }
  60. }
  61. /* ValidInt returns error if field does not contain a valid integer value */
  62. func ValidInt(field FormField, ctx context.Context) error {
  63. _, err := field.GetInt()
  64. if err != nil {
  65. return EInvalidInteger
  66. }
  67. return nil
  68. }
  69. /* ValidBetween makes sure that field is integer value within the specified range. */
  70. func ValidBetween(min, max int) ValidatorFunc {
  71. var EInvalidInterval = errors.New(
  72. fmt.Sprintf("must be integer between %d and %d", min, max))
  73. return func(field FormField, ctx context.Context) error {
  74. value, err := field.GetInt()
  75. if err != nil {
  76. return EInvalidInteger
  77. }
  78. if value < min || value > max {
  79. return EInvalidInterval
  80. }
  81. return nil
  82. }
  83. }
  84. /* ValidFloat returns error if field does not contain a valid integer value */
  85. func ValidFloat(field FormField, ctx context.Context) error {
  86. _, err := field.GetFloat()
  87. if err != nil {
  88. return EInvalidFloat
  89. }
  90. return nil
  91. }
  92. /* ValidBetweenFloat32 makes sure field is float64 value within the specified range. */
  93. func ValidBetweenFloat(min, max float64) ValidatorFunc {
  94. var EInvalidInterval = errors.New(
  95. fmt.Sprintf("must be float value between %.2f and %.2f", min, max))
  96. return func(field FormField, ctx context.Context) error {
  97. value, err := field.GetFloat()
  98. if err != nil {
  99. return err
  100. }
  101. if value < min || value > max {
  102. return EInvalidInterval
  103. }
  104. return nil
  105. }
  106. }
  107. /* ValidFieldEqualTo is a validator that checks if two fields have the same value. */
  108. func ValidFieldEqualTo(Other *FormField, err error) ValidatorFunc {
  109. return func(field FormField, ctx context.Context) error {
  110. if field.GetString() != Other.GetString() {
  111. return err
  112. }
  113. return nil
  114. }
  115. }