validator.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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. func NotEmpty() string {
  29. return "notEmpty"
  30. }
  31. func Lt(mark string) string {
  32. return "lt=" + mark
  33. }
  34. func Le(mark string) string {
  35. return "le=" + mark
  36. }
  37. func Eq(mark string) string {
  38. return "eq=" + mark
  39. }
  40. func Ne(mark string) string {
  41. return "ne=" + mark
  42. }
  43. func Ge(mark string) string {
  44. return "ge=" + mark
  45. }
  46. func Gt(mark string) string {
  47. return "gt=" + mark
  48. }
  49. func Verify(st interface{}, roleMap Rules,lang string) (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. for i := 0; i < num; i++ {
  66. tagVal := typ.Field(i)
  67. val := val.Field(i)
  68. tagName := tagVal.Tag.Get("form")
  69. if tagName == ""{
  70. tagName = tagVal.Name
  71. }
  72. if len(roleMap[tagVal.Name]) > 0 {
  73. for _, v := range roleMap[tagVal.Name] {
  74. switch {
  75. case v == "notEmpty":
  76. if isBlank(val) {
  77. return errors.New(tagName + getVerifyErrInfo(VERIFY_NOT_EMPTY,lang))
  78. }
  79. case compareMap[strings.Split(v, "=")[0]]:
  80. if !compareVerify(val, v) {
  81. return errors.New(tagName + getVerifyErrInfo(VERIFY_LENGTH_NOT_LEGA,lang) + v)
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. func compareVerify(value reflect.Value, VerifyStr string) bool {
  90. switch value.Kind() {
  91. case reflect.String, reflect.Slice, reflect.Array:
  92. return compare(value.Len(), VerifyStr)
  93. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  94. return compare(value.Uint(), VerifyStr)
  95. case reflect.Float32, reflect.Float64:
  96. return compare(value.Float(), VerifyStr)
  97. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  98. return compare(value.Int(), VerifyStr)
  99. default:
  100. return false
  101. }
  102. }
  103. func isBlank(value reflect.Value) bool {
  104. switch value.Kind() {
  105. case reflect.String:
  106. return value.Len() == 0
  107. case reflect.Bool:
  108. return !value.Bool()
  109. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  110. return value.Int() == 0
  111. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  112. return value.Uint() == 0
  113. case reflect.Float32, reflect.Float64:
  114. return value.Float() == 0
  115. case reflect.Interface, reflect.Ptr:
  116. return value.IsNil()
  117. }
  118. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  119. }
  120. func compare(value interface{}, VerifyStr string) bool {
  121. VerifyStrArr := strings.Split(VerifyStr, "=")
  122. val := reflect.ValueOf(value)
  123. switch val.Kind() {
  124. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  125. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  126. if VErr != nil {
  127. return false
  128. }
  129. switch {
  130. case VerifyStrArr[0] == "lt":
  131. return val.Int() < VInt
  132. case VerifyStrArr[0] == "le":
  133. return val.Int() <= VInt
  134. case VerifyStrArr[0] == "eq":
  135. return val.Int() == VInt
  136. case VerifyStrArr[0] == "ne":
  137. return val.Int() != VInt
  138. case VerifyStrArr[0] == "ge":
  139. return val.Int() >= VInt
  140. case VerifyStrArr[0] == "gt":
  141. return val.Int() > VInt
  142. default:
  143. return false
  144. }
  145. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  146. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  147. if VErr != nil {
  148. return false
  149. }
  150. switch {
  151. case VerifyStrArr[0] == "lt":
  152. return val.Uint() < uint64(VInt)
  153. case VerifyStrArr[0] == "le":
  154. return val.Uint() <= uint64(VInt)
  155. case VerifyStrArr[0] == "eq":
  156. return val.Uint() == uint64(VInt)
  157. case VerifyStrArr[0] == "ne":
  158. return val.Uint() != uint64(VInt)
  159. case VerifyStrArr[0] == "ge":
  160. return val.Uint() >= uint64(VInt)
  161. case VerifyStrArr[0] == "gt":
  162. return val.Uint() > uint64(VInt)
  163. default:
  164. return false
  165. }
  166. case reflect.Float32, reflect.Float64:
  167. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  168. if VErr != nil {
  169. return false
  170. }
  171. switch {
  172. case VerifyStrArr[0] == "lt":
  173. return val.Float() < VFloat
  174. case VerifyStrArr[0] == "le":
  175. return val.Float() <= VFloat
  176. case VerifyStrArr[0] == "eq":
  177. return val.Float() == VFloat
  178. case VerifyStrArr[0] == "ne":
  179. return val.Float() != VFloat
  180. case VerifyStrArr[0] == "ge":
  181. return val.Float() >= VFloat
  182. case VerifyStrArr[0] == "gt":
  183. return val.Float() > VFloat
  184. default:
  185. return false
  186. }
  187. default:
  188. return false
  189. }
  190. }
  191. func getVerifyErrInfo(errType string,lang string) (errInfo string) {
  192. if lang == LANG_EN{
  193. switch errType {
  194. case VERIFY_NOT_EMPTY:
  195. errInfo = " Cannot be empty"
  196. case VERIFY_LENGTH_NOT_LEGA:
  197. errInfo = " Length or value is not in legal range,"
  198. }
  199. }else{
  200. switch errType {
  201. case VERIFY_NOT_EMPTY:
  202. errInfo = " 值不能为空"
  203. case VERIFY_LENGTH_NOT_LEGA:
  204. errInfo = " 长度或值不在合法范围,"
  205. }
  206. }
  207. return
  208. }
  209. func ContainsChinese(str string) bool {
  210. var reg = regexp.MustCompile("^[\u4e00-\u9fa5]$")
  211. for _, v := range str {
  212. if reg.MatchString(string(v)) {
  213. return true
  214. }
  215. }
  216. return false
  217. }