validate.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package domainservice
  2. import (
  3. "context"
  4. "mime/multipart"
  5. "regexp"
  6. "strings"
  7. "Cold_Logistic/internal/pkg/common/codex"
  8. "github.com/dlclark/regexp2"
  9. validation "github.com/go-ozzo/ozzo-validation/v4"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  11. )
  12. func ValidationAccountNum() validation.RuleWithContextFunc {
  13. return func(ctx context.Context, value interface{}) error {
  14. reg, err := regexp2.Compile(`^((?=.*[0-9])(?=.*[a-zA-Z]).{6,50})$`, 0)
  15. if err != nil {
  16. return err
  17. }
  18. m, err := reg.MatchString(value.(string))
  19. if err != nil {
  20. return err
  21. }
  22. if !m {
  23. return errors.New("可以是手机号码或者包含字符和数字,至少6位")
  24. }
  25. return nil
  26. }
  27. }
  28. func ValidationIdentityNumber() validation.RuleWithContextFunc {
  29. return func(ctx context.Context, value interface{}) error {
  30. regx := "((^[1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}([0-9]|(X|x)))|(^[1-9][0-9]{5}[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{2}[0-9]))"
  31. err := validation.Validate(value, validation.Match(regexp.MustCompile(regx)).Error("身份证号码不合规"))
  32. if err != nil {
  33. return err
  34. }
  35. return nil
  36. }
  37. }
  38. func ValidationExcelFile(fileHeader *multipart.FileHeader, limitSize int64) error {
  39. if !strings.HasSuffix(fileHeader.Filename, "xlsx") {
  40. return errors.WithCode(codex.ErrParamValidate, "文件扩展名必须是xlsx")
  41. }
  42. if fileHeader.Size > limitSize {
  43. return errors.WithCode(codex.ErrParamValidate, "文件大小不能超过10M")
  44. }
  45. return nil
  46. }