validator.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 tagVal.Type.Kind() == reflect.Struct {
  115. if err = Verify(val.Interface(), roleMap); err != nil {
  116. return err
  117. }
  118. }
  119. if len(roleMap[tagVal.Name]) > 0 {
  120. for _, v := range roleMap[tagVal.Name] {
  121. switch {
  122. case v == "notEmpty":
  123. if isBlank(val) {
  124. return errors.New(tagVal.Name + "值不能为空")
  125. }
  126. case strings.Split(v, "=")[0] == "regexp":
  127. if !regexpMatch(strings.Split(v, "=")[1], val.String()) {
  128. return errors.New(tagVal.Name + "格式校验不通过")
  129. }
  130. case compareMap[strings.Split(v, "=")[0]]:
  131. if !compareVerify(val, v) {
  132. return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return nil
  139. }
  140. //@author: [piexlmax](https://github.com/piexlmax)
  141. //@function: compareVerify
  142. //@description: 长度和数字的校验方法 根据类型自动校验
  143. //@param: value reflect.Value, VerifyStr string
  144. //@return: bool
  145. func compareVerify(value reflect.Value, VerifyStr string) bool {
  146. switch value.Kind() {
  147. case reflect.String:
  148. return compare(len([]rune(value.String())), VerifyStr)
  149. case reflect.Slice, reflect.Array:
  150. return compare(value.Len(), VerifyStr)
  151. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  152. return compare(value.Uint(), VerifyStr)
  153. case reflect.Float32, reflect.Float64:
  154. return compare(value.Float(), VerifyStr)
  155. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  156. return compare(value.Int(), VerifyStr)
  157. default:
  158. return false
  159. }
  160. }
  161. //@author: [piexlmax](https://github.com/piexlmax)
  162. //@function: isBlank
  163. //@description: 非空校验
  164. //@param: value reflect.Value
  165. //@return: bool
  166. func isBlank(value reflect.Value) bool {
  167. switch value.Kind() {
  168. case reflect.String, reflect.Slice:
  169. return value.Len() == 0
  170. case reflect.Bool:
  171. return !value.Bool()
  172. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  173. return value.Int() == 0
  174. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  175. return value.Uint() == 0
  176. case reflect.Float32, reflect.Float64:
  177. return value.Float() == 0
  178. case reflect.Interface, reflect.Ptr:
  179. return value.IsNil()
  180. }
  181. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  182. }
  183. //@author: [piexlmax](https://github.com/piexlmax)
  184. //@function: compare
  185. //@description: 比较函数
  186. //@param: value interface{}, VerifyStr string
  187. //@return: bool
  188. func compare(value interface{}, VerifyStr string) bool {
  189. VerifyStrArr := strings.Split(VerifyStr, "=")
  190. val := reflect.ValueOf(value)
  191. switch val.Kind() {
  192. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  193. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  194. if VErr != nil {
  195. return false
  196. }
  197. switch {
  198. case VerifyStrArr[0] == "lt":
  199. return val.Int() < VInt
  200. case VerifyStrArr[0] == "le":
  201. return val.Int() <= VInt
  202. case VerifyStrArr[0] == "eq":
  203. return val.Int() == VInt
  204. case VerifyStrArr[0] == "ne":
  205. return val.Int() != VInt
  206. case VerifyStrArr[0] == "ge":
  207. return val.Int() >= VInt
  208. case VerifyStrArr[0] == "gt":
  209. return val.Int() > VInt
  210. default:
  211. return false
  212. }
  213. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  214. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  215. if VErr != nil {
  216. return false
  217. }
  218. switch {
  219. case VerifyStrArr[0] == "lt":
  220. return val.Uint() < uint64(VInt)
  221. case VerifyStrArr[0] == "le":
  222. return val.Uint() <= uint64(VInt)
  223. case VerifyStrArr[0] == "eq":
  224. return val.Uint() == uint64(VInt)
  225. case VerifyStrArr[0] == "ne":
  226. return val.Uint() != uint64(VInt)
  227. case VerifyStrArr[0] == "ge":
  228. return val.Uint() >= uint64(VInt)
  229. case VerifyStrArr[0] == "gt":
  230. return val.Uint() > uint64(VInt)
  231. default:
  232. return false
  233. }
  234. case reflect.Float32, reflect.Float64:
  235. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  236. if VErr != nil {
  237. return false
  238. }
  239. switch {
  240. case VerifyStrArr[0] == "lt":
  241. return val.Float() < VFloat
  242. case VerifyStrArr[0] == "le":
  243. return val.Float() <= VFloat
  244. case VerifyStrArr[0] == "eq":
  245. return val.Float() == VFloat
  246. case VerifyStrArr[0] == "ne":
  247. return val.Float() != VFloat
  248. case VerifyStrArr[0] == "ge":
  249. return val.Float() >= VFloat
  250. case VerifyStrArr[0] == "gt":
  251. return val.Float() > VFloat
  252. default:
  253. return false
  254. }
  255. default:
  256. return false
  257. }
  258. }
  259. func regexpMatch(rule, matchStr string) bool {
  260. return regexp.MustCompile(rule).MatchString(matchStr)
  261. }