validator.go 6.4 KB

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