A fast pacakge for validation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package validate
  2. import (
  3. "reflect"
  4. "regexp"
  5. "strings"
  6. )
  7. // ValidationFunc is a function to validate a field
  8. type ValidationFunc func(reflect.Value, interface{}) bool
  9. // Kinds is a map with validation funcs for each reflect.Kind
  10. type Kinds map[reflect.Kind]ValidationFunc
  11. type listFuncInfo struct {
  12. inputFunc InputFunc
  13. kinds Kinds
  14. }
  15. // AddRule adds a rule to the list of validation functions
  16. func AddRule(name string, inputFunc InputFunc, kinds Kinds) {
  17. funcs[name] = listFuncInfo{inputFunc, kinds}
  18. }
  19. // nolint: dupl
  20. var funcs = map[string]listFuncInfo{
  21. `required`: {nil, Kinds{
  22. reflect.Ptr: func(rv reflect.Value, _ interface{}) bool {
  23. return !rv.IsNil()
  24. },
  25. reflect.Interface: func(rv reflect.Value, _ interface{}) bool {
  26. return !rv.IsNil()
  27. },
  28. reflect.Slice: func(rv reflect.Value, _ interface{}) bool {
  29. return !rv.IsNil() && rv.Len() > 0
  30. },
  31. reflect.Map: func(rv reflect.Value, _ interface{}) bool {
  32. return !rv.IsNil() && rv.Len() > 0
  33. },
  34. reflect.String: func(rv reflect.Value, _ interface{}) bool {
  35. return rv.String() != ``
  36. },
  37. reflect.Int: func(rv reflect.Value, _ interface{}) bool {
  38. return rv.Int() != 0
  39. },
  40. reflect.Uint: func(rv reflect.Value, _ interface{}) bool {
  41. return rv.Uint() != 0
  42. },
  43. reflect.Float64: func(rv reflect.Value, _ interface{}) bool {
  44. return rv.Float() != 0
  45. },
  46. }},
  47. // Strings
  48. `prefix`: {InputSame, Kinds{
  49. reflect.String: func(rv reflect.Value, val interface{}) bool {
  50. return strings.HasPrefix(rv.String(), val.(string))
  51. },
  52. }},
  53. `suffix`: {InputSame, Kinds{
  54. reflect.String: func(rv reflect.Value, val interface{}) bool {
  55. return strings.HasSuffix(rv.String(), val.(string))
  56. },
  57. }},
  58. `contains`: {InputSame, Kinds{
  59. reflect.String: func(rv reflect.Value, val interface{}) bool {
  60. return strings.Contains(rv.String(), val.(string))
  61. },
  62. }},
  63. `regexp`: {InputRegexp, Kinds{
  64. reflect.String: func(rv reflect.Value, val interface{}) bool {
  65. return val.(*regexp.Regexp).MatchString(rv.String())
  66. },
  67. }},
  68. // Comparisons
  69. `eq`: {InputSame, Kinds{
  70. reflect.String: func(rv reflect.Value, val interface{}) bool {
  71. return rv.String() == val.(string)
  72. },
  73. reflect.Int: func(rv reflect.Value, val interface{}) bool {
  74. return rv.Int() == val.(int64)
  75. },
  76. reflect.Uint: func(rv reflect.Value, val interface{}) bool {
  77. return rv.Uint() == val.(uint64)
  78. },
  79. reflect.Float64: func(rv reflect.Value, val interface{}) bool {
  80. return rv.Float() == val.(float64)
  81. },
  82. }},
  83. // Integers
  84. `gt`: {InputSame, Kinds{
  85. reflect.Int: func(rv reflect.Value, val interface{}) bool {
  86. return rv.Int() > val.(int64)
  87. },
  88. reflect.Uint: func(rv reflect.Value, val interface{}) bool {
  89. return rv.Uint() > val.(uint64)
  90. },
  91. reflect.Float64: func(rv reflect.Value, val interface{}) bool {
  92. return rv.Float() > val.(float64)
  93. },
  94. }},
  95. `lt`: {InputSame, Kinds{
  96. reflect.Int: func(rv reflect.Value, val interface{}) bool {
  97. return rv.Int() < val.(int64)
  98. },
  99. reflect.Uint: func(rv reflect.Value, val interface{}) bool {
  100. return rv.Uint() < val.(uint64)
  101. },
  102. reflect.Float64: func(rv reflect.Value, val interface{}) bool {
  103. return rv.Float() < val.(float64)
  104. },
  105. }},
  106. // Slices, maps & strings
  107. `len`: {InputInt, Kinds{
  108. reflect.Slice: func(rv reflect.Value, val interface{}) bool {
  109. return rv.Len() == val.(int)
  110. },
  111. reflect.Map: func(rv reflect.Value, val interface{}) bool {
  112. return rv.Len() == val.(int)
  113. },
  114. reflect.String: func(rv reflect.Value, val interface{}) bool {
  115. return rv.Len() == val.(int)
  116. },
  117. }},
  118. `min`: {InputInt, Kinds{
  119. reflect.Slice: func(rv reflect.Value, val interface{}) bool {
  120. return rv.Len() >= val.(int)
  121. },
  122. reflect.Map: func(rv reflect.Value, val interface{}) bool {
  123. return rv.Len() >= val.(int)
  124. },
  125. reflect.String: func(rv reflect.Value, val interface{}) bool {
  126. return rv.Len() >= val.(int)
  127. },
  128. }},
  129. `max`: {InputInt, Kinds{
  130. reflect.Slice: func(rv reflect.Value, val interface{}) bool {
  131. return rv.Len() <= val.(int)
  132. },
  133. reflect.Map: func(rv reflect.Value, val interface{}) bool {
  134. return rv.Len() <= val.(int)
  135. },
  136. reflect.String: func(rv reflect.Value, val interface{}) bool {
  137. return rv.Len() <= val.(int)
  138. },
  139. }},
  140. }