validator.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package utils
  2. import (
  3. "errors"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. type Rules map[string][]string
  9. type RulesMap map[string]Rules
  10. var CustomizeMap = make(map[string]Rules)
  11. // 注册自定义规则方案建议在路由初始化层即注册
  12. func RegisterRule(key string, rule Rules) (err error) {
  13. if CustomizeMap[key] != nil {
  14. return errors.New(key + "已注册,无法重复注册")
  15. } else {
  16. CustomizeMap[key] = rule
  17. return nil
  18. }
  19. }
  20. // 非空 不能为其对应类型的0值
  21. func NotEmpty() string {
  22. return "notEmpty"
  23. }
  24. // 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  25. func Lt(mark string) string {
  26. return "lt=" + mark
  27. }
  28. // 小于等于入参(<=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  29. func Le(mark string) string {
  30. return "le=" + mark
  31. }
  32. // 等于入参(==) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  33. func Eq(mark string) string {
  34. return "eq=" + mark
  35. }
  36. // 不等于入参(!=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  37. func Ne(mark string) string {
  38. return "ne=" + mark
  39. }
  40. // 大于等于入参(>=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  41. func Ge(mark string) string {
  42. return "ge=" + mark
  43. }
  44. // 大于入参(>) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  45. func Gt(mark string) string {
  46. return "gt=" + mark
  47. }
  48. // 校验方法 接收两个参数 入参实例,规则map
  49. func Verify(st interface{}, roleMap Rules) (err error) {
  50. compareMap := map[string]bool{
  51. "lt": true,
  52. "le": true,
  53. "eq": true,
  54. "ne": true,
  55. "ge": true,
  56. "gt": true,
  57. }
  58. typ := reflect.TypeOf(st)
  59. val := reflect.ValueOf(st) // 获取reflect.Type类型
  60. kd := val.Kind() // 获取到st对应的类别
  61. if kd != reflect.Struct {
  62. return errors.New("expect struct")
  63. }
  64. num := val.NumField()
  65. // 遍历结构体的所有字段
  66. for i := 0; i < num; i++ {
  67. tagVal := typ.Field(i)
  68. val := val.Field(i)
  69. //如果有填写form字段的话,那么返回提示使用该字段
  70. tagName := tagVal.Tag.Get("form")
  71. if tagName == "" {
  72. tagName = tagVal.Name
  73. }
  74. if len(roleMap[tagVal.Name]) > 0 {
  75. for _, v := range roleMap[tagVal.Name] {
  76. switch {
  77. case v == "notEmpty":
  78. if isBlank(val) {
  79. return errors.New(tagName + "值不能为空")
  80. }
  81. case compareMap[strings.Split(v, "=")[0]]:
  82. if !compareVerify(val, v) {
  83. return errors.New(tagName + "长度或值不在合法范围," + v)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return nil
  90. }
  91. // 长度和数字的校验方法 根据类型自动校验
  92. func compareVerify(value reflect.Value, VerifyStr string) bool {
  93. switch value.Kind() {
  94. case reflect.String, reflect.Slice, reflect.Array:
  95. return compare(value.Len(), VerifyStr)
  96. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  97. return compare(value.Uint(), VerifyStr)
  98. case reflect.Float32, reflect.Float64:
  99. return compare(value.Float(), VerifyStr)
  100. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  101. return compare(value.Int(), VerifyStr)
  102. default:
  103. return false
  104. }
  105. }
  106. // 非空校验
  107. func isBlank(value reflect.Value) bool {
  108. switch value.Kind() {
  109. case reflect.String:
  110. return value.Len() == 0
  111. case reflect.Bool:
  112. return !value.Bool()
  113. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  114. return value.Int() == 0
  115. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  116. return value.Uint() == 0
  117. case reflect.Float32, reflect.Float64:
  118. return value.Float() == 0
  119. case reflect.Interface, reflect.Ptr:
  120. return value.IsNil()
  121. }
  122. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  123. }
  124. func compare(value interface{}, VerifyStr string) bool {
  125. VerifyStrArr := strings.Split(VerifyStr, "=")
  126. val := reflect.ValueOf(value)
  127. switch val.Kind() {
  128. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  129. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  130. if VErr != nil {
  131. return false
  132. }
  133. switch {
  134. case VerifyStrArr[0] == "lt":
  135. return val.Int() < VInt
  136. case VerifyStrArr[0] == "le":
  137. return val.Int() <= VInt
  138. case VerifyStrArr[0] == "eq":
  139. return val.Int() == VInt
  140. case VerifyStrArr[0] == "ne":
  141. return val.Int() != VInt
  142. case VerifyStrArr[0] == "ge":
  143. return val.Int() >= VInt
  144. case VerifyStrArr[0] == "gt":
  145. return val.Int() > VInt
  146. default:
  147. return false
  148. }
  149. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  150. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  151. if VErr != nil {
  152. return false
  153. }
  154. switch {
  155. case VerifyStrArr[0] == "lt":
  156. return val.Uint() < uint64(VInt)
  157. case VerifyStrArr[0] == "le":
  158. return val.Uint() <= uint64(VInt)
  159. case VerifyStrArr[0] == "eq":
  160. return val.Uint() == uint64(VInt)
  161. case VerifyStrArr[0] == "ne":
  162. return val.Uint() != uint64(VInt)
  163. case VerifyStrArr[0] == "ge":
  164. return val.Uint() >= uint64(VInt)
  165. case VerifyStrArr[0] == "gt":
  166. return val.Uint() > uint64(VInt)
  167. default:
  168. return false
  169. }
  170. case reflect.Float32, reflect.Float64:
  171. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  172. if VErr != nil {
  173. return false
  174. }
  175. switch {
  176. case VerifyStrArr[0] == "lt":
  177. return val.Float() < VFloat
  178. case VerifyStrArr[0] == "le":
  179. return val.Float() <= VFloat
  180. case VerifyStrArr[0] == "eq":
  181. return val.Float() == VFloat
  182. case VerifyStrArr[0] == "ne":
  183. return val.Float() != VFloat
  184. case VerifyStrArr[0] == "ge":
  185. return val.Float() >= VFloat
  186. case VerifyStrArr[0] == "gt":
  187. return val.Float() > VFloat
  188. default:
  189. return false
  190. }
  191. default:
  192. return false
  193. }
  194. }