validator.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. var (
  13. LANG_CN = "cn" //中文
  14. LANG_EN = "english" //英文
  15. )
  16. var (
  17. VERIFY_NOT_EMPTY = "notEmpty" //不为空,必填
  18. VERIFY_LENGTH_NOT_LEGA = "lengthNotLega" //长度不合规
  19. )
  20. // 注册自定义规则方案建议在路由初始化层即注册
  21. func RegisterRule(key string, rule Rules) (err error) {
  22. if CustomizeMap[key] != nil {
  23. return errors.New(key + "已注册,无法重复注册")
  24. } else {
  25. CustomizeMap[key] = rule
  26. return nil
  27. }
  28. }
  29. // 非空 不能为其对应类型的0值
  30. func NotEmpty() string {
  31. return "notEmpty"
  32. }
  33. // 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  34. func Lt(mark string) string {
  35. return "lt=" + mark
  36. }
  37. // 小于等于入参(<=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  38. func Le(mark string) string {
  39. return "le=" + mark
  40. }
  41. // 等于入参(==) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  42. func Eq(mark string) string {
  43. return "eq=" + mark
  44. }
  45. // 不等于入参(!=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  46. func Ne(mark string) string {
  47. return "ne=" + mark
  48. }
  49. // 大于等于入参(>=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  50. func Ge(mark string) string {
  51. return "ge=" + mark
  52. }
  53. // 大于入参(>) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  54. func Gt(mark string) string {
  55. return "gt=" + mark
  56. }
  57. // 校验方法 接收两个参数 入参实例,规则map
  58. func Verify(st interface{}, roleMap Rules,lang string) (err error) {
  59. compareMap := map[string]bool{
  60. "lt": true,
  61. "le": true,
  62. "eq": true,
  63. "ne": true,
  64. "ge": true,
  65. "gt": true,
  66. }
  67. typ := reflect.TypeOf(st)
  68. val := reflect.ValueOf(st) // 获取reflect.Type类型
  69. kd := val.Kind() // 获取到st对应的类别
  70. if kd != reflect.Struct {
  71. return errors.New("expect struct")
  72. }
  73. num := val.NumField()
  74. // 遍历结构体的所有字段
  75. for i := 0; i < num; i++ {
  76. tagVal := typ.Field(i)
  77. val := val.Field(i)
  78. //如果有填写form字段的话,那么返回提示使用该字段
  79. tagName := tagVal.Tag.Get("form")
  80. if tagName == ""{
  81. tagName = tagVal.Name
  82. }
  83. if len(roleMap[tagVal.Name]) > 0 {
  84. for _, v := range roleMap[tagVal.Name] {
  85. switch {
  86. case v == "notEmpty":
  87. if isBlank(val) {
  88. return errors.New(tagName + getVerifyErrInfo(VERIFY_NOT_EMPTY,lang))
  89. }
  90. case compareMap[strings.Split(v, "=")[0]]:
  91. if !compareVerify(val, v) {
  92. return errors.New(tagName + getVerifyErrInfo(VERIFY_LENGTH_NOT_LEGA,lang) + v)
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return nil
  99. }
  100. // 长度和数字的校验方法 根据类型自动校验
  101. func compareVerify(value reflect.Value, VerifyStr string) bool {
  102. switch value.Kind() {
  103. case reflect.String, reflect.Slice, reflect.Array:
  104. return compare(value.Len(), VerifyStr)
  105. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  106. return compare(value.Uint(), VerifyStr)
  107. case reflect.Float32, reflect.Float64:
  108. return compare(value.Float(), VerifyStr)
  109. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  110. return compare(value.Int(), VerifyStr)
  111. default:
  112. return false
  113. }
  114. }
  115. // 非空校验
  116. func isBlank(value reflect.Value) bool {
  117. switch value.Kind() {
  118. case reflect.String:
  119. return value.Len() == 0
  120. case reflect.Bool:
  121. return !value.Bool()
  122. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  123. return value.Int() == 0
  124. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  125. return value.Uint() == 0
  126. case reflect.Float32, reflect.Float64:
  127. return value.Float() == 0
  128. case reflect.Interface, reflect.Ptr:
  129. return value.IsNil()
  130. }
  131. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  132. }
  133. func compare(value interface{}, VerifyStr string) bool {
  134. VerifyStrArr := strings.Split(VerifyStr, "=")
  135. val := reflect.ValueOf(value)
  136. switch val.Kind() {
  137. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  138. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  139. if VErr != nil {
  140. return false
  141. }
  142. switch {
  143. case VerifyStrArr[0] == "lt":
  144. return val.Int() < VInt
  145. case VerifyStrArr[0] == "le":
  146. return val.Int() <= VInt
  147. case VerifyStrArr[0] == "eq":
  148. return val.Int() == VInt
  149. case VerifyStrArr[0] == "ne":
  150. return val.Int() != VInt
  151. case VerifyStrArr[0] == "ge":
  152. return val.Int() >= VInt
  153. case VerifyStrArr[0] == "gt":
  154. return val.Int() > VInt
  155. default:
  156. return false
  157. }
  158. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  159. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  160. if VErr != nil {
  161. return false
  162. }
  163. switch {
  164. case VerifyStrArr[0] == "lt":
  165. return val.Uint() < uint64(VInt)
  166. case VerifyStrArr[0] == "le":
  167. return val.Uint() <= uint64(VInt)
  168. case VerifyStrArr[0] == "eq":
  169. return val.Uint() == uint64(VInt)
  170. case VerifyStrArr[0] == "ne":
  171. return val.Uint() != uint64(VInt)
  172. case VerifyStrArr[0] == "ge":
  173. return val.Uint() >= uint64(VInt)
  174. case VerifyStrArr[0] == "gt":
  175. return val.Uint() > uint64(VInt)
  176. default:
  177. return false
  178. }
  179. case reflect.Float32, reflect.Float64:
  180. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  181. if VErr != nil {
  182. return false
  183. }
  184. switch {
  185. case VerifyStrArr[0] == "lt":
  186. return val.Float() < VFloat
  187. case VerifyStrArr[0] == "le":
  188. return val.Float() <= VFloat
  189. case VerifyStrArr[0] == "eq":
  190. return val.Float() == VFloat
  191. case VerifyStrArr[0] == "ne":
  192. return val.Float() != VFloat
  193. case VerifyStrArr[0] == "ge":
  194. return val.Float() >= VFloat
  195. case VerifyStrArr[0] == "gt":
  196. return val.Float() > VFloat
  197. default:
  198. return false
  199. }
  200. default:
  201. return false
  202. }
  203. }
  204. //获取错误信息(中文文)
  205. func getVerifyErrInfo(errType string,lang string) (errInfo string) {
  206. if lang == LANG_EN{
  207. switch errType {
  208. case VERIFY_NOT_EMPTY:
  209. errInfo = " Cannot be empty"
  210. case VERIFY_LENGTH_NOT_LEGA:
  211. errInfo = " Length or value is not in legal range,"
  212. }
  213. }else{
  214. switch errType {
  215. case VERIFY_NOT_EMPTY:
  216. errInfo = " 值不能为空"
  217. case VERIFY_LENGTH_NOT_LEGA:
  218. errInfo = " 长度或值不在合法范围,"
  219. }
  220. }
  221. return
  222. }
  223. func ContainsChinese(str string) bool {
  224. //设定一个含有中文的字符串
  225. var reg = regexp.MustCompile("^[\u4e00-\u9fa5]$")
  226. //接受正则表达式的范围
  227. for _, v := range str {
  228. //golang中string的底层是byte类型,所以单纯的for输出中文会出现乱码,这里选择for-range来输出
  229. if reg.MatchString(string(v)) {
  230. //判断是否为中文,如果是返回一个true,不是返回false。这俩面MatchString的参数要求是string
  231. return true
  232. }
  233. }
  234. return false
  235. }