github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/matcher/model.go (about)

     1  package matcher
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/constants"
     6  	"reflect"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/isyscore/isc-gobase/logger"
    11  )
    12  
    13  type ModelMatch struct {
    14  	BlackWhiteMatch
    15  
    16  	isIdCard  bool
    17  	modelName string
    18  	pReg      *regexp.Regexp
    19  }
    20  
    21  var modelMap = map[string]*regexp.Regexp{}
    22  
    23  func (modelMatch *ModelMatch) Match(_ map[string]interface{}, _ any, field reflect.StructField, fieldValue any) bool {
    24  	if nil == fieldValue {
    25  		return false
    26  	}
    27  
    28  	if field.Type.Kind() != reflect.String {
    29  		return false
    30  	}
    31  
    32  	// 身份证号单独处理
    33  	if modelMatch.isIdCard {
    34  		if idCardIsValidate(fmt.Sprintf("%v", fieldValue)) {
    35  			modelMatch.SetBlackMsg("属性 %v 的值 %v 符合身份证要求", field.Name, fieldValue)
    36  			return true
    37  		} else {
    38  			modelMatch.SetWhiteMsg("属性 %v 的值 %v 不符合身份证要求", field.Name, fieldValue)
    39  			return false
    40  		}
    41  	} else {
    42  		if modelMatch.pReg.MatchString(fmt.Sprintf("%v", fieldValue)) {
    43  			modelMatch.SetBlackMsg("属性 %v 的值 %v 命中不允许的类型 [%v]", field.Name, fieldValue, modelMatch.modelName)
    44  			return true
    45  		} else {
    46  			modelMatch.SetWhiteMsg("属性 %v 的值 %v 没有命中只允许类型 [%v]", field.Name, fieldValue, modelMatch.modelName)
    47  			return false
    48  		}
    49  	}
    50  }
    51  
    52  func (modelMatch *ModelMatch) IsEmpty() bool {
    53  	if modelMatch.isIdCard {
    54  		return false
    55  	}
    56  	return modelMatch.pReg == nil
    57  }
    58  
    59  func BuildModelMatcher(objectTypeFullName string, fieldKind reflect.Kind, objectFieldName string, tagName string, subCondition string, errMsg string) {
    60  	if constants.MATCH != tagName {
    61  		return
    62  	}
    63  
    64  	if fieldKind == reflect.Slice {
    65  		return
    66  	}
    67  
    68  	if !strings.Contains(subCondition, constants.Model) || !strings.Contains(subCondition, constants.EQUAL) {
    69  		return
    70  	}
    71  
    72  	index := strings.Index(subCondition, "=")
    73  	modelKey := strings.TrimSpace(subCondition[index+1:])
    74  
    75  	pReg, contain := modelMap[modelKey]
    76  	if !contain && modelKey != constants.IdCard {
    77  		logger.Error("不包含模式%v", modelKey)
    78  		return
    79  	}
    80  
    81  	if modelKey == constants.IdCard {
    82  		addMatcher(objectTypeFullName, objectFieldName, &ModelMatch{pReg: pReg, isIdCard: true, modelName: modelKey}, errMsg, true)
    83  	} else {
    84  		addMatcher(objectTypeFullName, objectFieldName, &ModelMatch{pReg: pReg, isIdCard: false, modelName: modelKey}, errMsg, true)
    85  	}
    86  }
    87  
    88  func init() {
    89  	// 手机号
    90  	pReg, _ := regexp.Compile("^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$")
    91  	modelMap[constants.Phone] = pReg
    92  
    93  	// 固定电话
    94  	pReg, _ = regexp.Compile("^(([0+]\\d{2,3}-)?(0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$")
    95  	modelMap[constants.FixedPhone] = pReg
    96  
    97  	// 邮箱
    98  	pReg, _ = regexp.Compile("^([\\w-_]+(?:\\.[\\w-_]+)*)@[\\w-]+(.[\\w_-]+)+")
    99  	modelMap[constants.MAIL] = pReg
   100  
   101  	// IP地址
   102  	pReg, _ = regexp.Compile("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$")
   103  	modelMap[constants.IpAddress] = pReg
   104  }