validator.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package utils
  2. import (
  3. "errors"
  4. "reflect"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. )
  9. type Rules map[string][]string
  10. type RulesMap map[string]Rules
  11. var CustomizeMap = make(map[string]Rules)
  12. //@author: [piexlmax](https://github.com/piexlmax)
  13. //@function: RegisterRule
  14. //@description: 注册自定义规则方案建议在路由初始化层即注册
  15. //@param: key string, rule Rules
  16. //@return: err error
  17. func RegisterRule(key string, rule Rules) (err error) {
  18. if CustomizeMap[key] != nil {
  19. return errors.New(key + "已注册,无法重复注册")
  20. } else {
  21. CustomizeMap[key] = rule
  22. return nil
  23. }
  24. }
  25. //@author: [piexlmax](https://github.com/piexlmax)
  26. //@function: NotEmpty
  27. //@description: 非空 不能为其对应类型的0值
  28. //@return: string
  29. func NotEmpty() string {
  30. return "notEmpty"
  31. }
  32. //@author: [zooqkl](https://github.com/zooqkl)
  33. //@function: RegexpMatch
  34. //@description: 正则校验 校验输入项是否满足正则表达式
  35. //@param: rule string
  36. //@return: string
  37. func RegexpMatch(rule string) string {
  38. return "regexp=" + rule
  39. }
  40. //@author: [piexlmax](https://github.com/piexlmax)
  41. //@function: Lt
  42. //@description: 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  43. //@param: mark string
  44. //@return: string
  45. func Lt(mark string) string {
  46. return "lt=" + mark
  47. }
  48. //@author: [piexlmax](https://github.com/piexlmax)
  49. //@function: Le
  50. //@description: 小于等于入参(<=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  51. //@param: mark string
  52. //@return: string
  53. func Le(mark string) string {
  54. return "le=" + mark
  55. }
  56. //@author: [piexlmax](https://github.com/piexlmax)
  57. //@function: Eq
  58. //@description: 等于入参(==) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  59. //@param: mark string
  60. //@return: string
  61. func Eq(mark string) string {
  62. return "eq=" + mark
  63. }
  64. //@author: [piexlmax](https://github.com/piexlmax)
  65. //@function: Ne
  66. //@description: 不等于入参(!=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  67. //@param: mark string
  68. //@return: string
  69. func Ne(mark string) string {
  70. return "ne=" + mark
  71. }
  72. //@author: [piexlmax](https://github.com/piexlmax)
  73. //@function: Ge
  74. //@description: 大于等于入参(>=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  75. //@param: mark string
  76. //@return: string
  77. func Ge(mark string) string {
  78. return "ge=" + mark
  79. }
  80. //@author: [piexlmax](https://github.com/piexlmax)
  81. //@function: Gt
  82. //@description: 大于入参(>) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  83. //@param: mark string
  84. //@return: string
  85. func Gt(mark string) string {
  86. return "gt=" + mark
  87. }
  88. //
  89. //@author: [piexlmax](https://github.com/piexlmax)
  90. //@function: Verify
  91. //@description: 校验方法
  92. //@param: st interface{}, roleMap Rules(入参实例,规则map)
  93. //@return: err error
  94. func Verify(st interface{}, roleMap Rules) (err error) {
  95. compareMap := map[string]bool{
  96. "lt": true,
  97. "le": true,
  98. "eq": true,
  99. "ne": true,
  100. "ge": true,
  101. "gt": true,
  102. }
  103. typ := reflect.TypeOf(st)
  104. val := reflect.ValueOf(st) // 获取reflect.Type类型
  105. kd := val.Kind() // 获取到st对应的类别
  106. if kd != reflect.Struct {
  107. return errors.New("expect struct")
  108. }
  109. num := val.NumField()
  110. // 遍历结构体的所有字段
  111. for i := 0; i < num; i++ {
  112. tagVal := typ.Field(i)
  113. val := val.Field(i)
  114. if len(roleMap[tagVal.Name]) > 0 {
  115. for _, v := range roleMap[tagVal.Name] {
  116. switch {
  117. case v == "notEmpty":
  118. if isBlank(val) {
  119. return errors.New(tagVal.Name + "值不能为空")
  120. }
  121. case strings.Split(v, "=")[0] == "regexp":
  122. if !regexpMatch(strings.Split(v, "=")[1], val.String()) {
  123. return errors.New(tagVal.Name + "格式校验不通过")
  124. }
  125. case compareMap[strings.Split(v, "=")[0]]:
  126. if !compareVerify(val, v) {
  127. return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
  128. }
  129. }
  130. }
  131. }
  132. }
  133. return nil
  134. }
  135. //@author: [piexlmax](https://github.com/piexlmax)
  136. //@function: compareVerify
  137. //@description: 长度和数字的校验方法 根据类型自动校验
  138. //@param: value reflect.Value, VerifyStr string
  139. //@return: bool
  140. func compareVerify(value reflect.Value, VerifyStr string) bool {
  141. switch value.Kind() {
  142. case reflect.String, reflect.Slice, reflect.Array:
  143. return compare(value.Len(), VerifyStr)
  144. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  145. return compare(value.Uint(), VerifyStr)
  146. case reflect.Float32, reflect.Float64:
  147. return compare(value.Float(), VerifyStr)
  148. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  149. return compare(value.Int(), VerifyStr)
  150. default:
  151. return false
  152. }
  153. }
  154. //@author: [piexlmax](https://github.com/piexlmax)
  155. //@function: isBlank
  156. //@description: 非空校验
  157. //@param: value reflect.Value
  158. //@return: bool
  159. func isBlank(value reflect.Value) bool {
  160. switch value.Kind() {
  161. case reflect.String:
  162. return value.Len() == 0
  163. case reflect.Bool:
  164. return !value.Bool()
  165. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  166. return value.Int() == 0
  167. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  168. return value.Uint() == 0
  169. case reflect.Float32, reflect.Float64:
  170. return value.Float() == 0
  171. case reflect.Interface, reflect.Ptr:
  172. return value.IsNil()
  173. }
  174. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  175. }
  176. //@author: [piexlmax](https://github.com/piexlmax)
  177. //@function: compare
  178. //@description: 比较函数
  179. //@param: value interface{}, VerifyStr string
  180. //@return: bool
  181. func compare(value interface{}, VerifyStr string) bool {
  182. VerifyStrArr := strings.Split(VerifyStr, "=")
  183. val := reflect.ValueOf(value)
  184. switch val.Kind() {
  185. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  186. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  187. if VErr != nil {
  188. return false
  189. }
  190. switch {
  191. case VerifyStrArr[0] == "lt":
  192. return val.Int() < VInt
  193. case VerifyStrArr[0] == "le":
  194. return val.Int() <= VInt
  195. case VerifyStrArr[0] == "eq":
  196. return val.Int() == VInt
  197. case VerifyStrArr[0] == "ne":
  198. return val.Int() != VInt
  199. case VerifyStrArr[0] == "ge":
  200. return val.Int() >= VInt
  201. case VerifyStrArr[0] == "gt":
  202. return val.Int() > VInt
  203. default:
  204. return false
  205. }
  206. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  207. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  208. if VErr != nil {
  209. return false
  210. }
  211. switch {
  212. case VerifyStrArr[0] == "lt":
  213. return val.Uint() < uint64(VInt)
  214. case VerifyStrArr[0] == "le":
  215. return val.Uint() <= uint64(VInt)
  216. case VerifyStrArr[0] == "eq":
  217. return val.Uint() == uint64(VInt)
  218. case VerifyStrArr[0] == "ne":
  219. return val.Uint() != uint64(VInt)
  220. case VerifyStrArr[0] == "ge":
  221. return val.Uint() >= uint64(VInt)
  222. case VerifyStrArr[0] == "gt":
  223. return val.Uint() > uint64(VInt)
  224. default:
  225. return false
  226. }
  227. case reflect.Float32, reflect.Float64:
  228. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  229. if VErr != nil {
  230. return false
  231. }
  232. switch {
  233. case VerifyStrArr[0] == "lt":
  234. return val.Float() < VFloat
  235. case VerifyStrArr[0] == "le":
  236. return val.Float() <= VFloat
  237. case VerifyStrArr[0] == "eq":
  238. return val.Float() == VFloat
  239. case VerifyStrArr[0] == "ne":
  240. return val.Float() != VFloat
  241. case VerifyStrArr[0] == "ge":
  242. return val.Float() >= VFloat
  243. case VerifyStrArr[0] == "gt":
  244. return val.Float() > VFloat
  245. default:
  246. return false
  247. }
  248. default:
  249. return false
  250. }
  251. }
  252. func regexpMatch(rule, matchStr string) bool {
  253. return regexp.MustCompile(rule).MatchString(matchStr)
  254. }